0
我的格式陣列這樣從陣列中獲得值用foreach
Array ( [0] => stdClass Object ([subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ([0] => stdClass Object ([subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290)) [subscriptionContractLogs] =>) [1] => stdClass Object ([subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ([0] => stdClass Object ([subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530) [1] => stdClass Object ([subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977)) [subscriptionContractLogs] =>) [2] => stdClass Object ([subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ([0] => stdClass Object ([subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267) [1] => stdClass Object ([subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687)) [subscriptionContractLogs] =>) [3] => stdClass Object ([subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ([0] => stdClass Object ([subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173) [1] => stdClass Object ([subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467)) [subscriptionContractLogs] =>)
我想利用這個值[subscriptionContractId] => 20859
PFB我的代碼
foreach($getContract as $key =>$value) { $cancel = $getContract[$key]->subscriptionContractId; $this->data['res'] = $cancel; }
上面的代碼只顯示記錄的單個值,同時記錄大約是27條記錄。
和另一個代碼,我嘗試用初始化數組PFB來糾正。
$cancel = array() foreach($getContract as $key =>$value) { $cancel[] = $getContract[$key]->subscriptionContractId; $this->data['res'] = $cancel; }
我只需要值,陣列如20879等的eventhough我使用雙迴路來提取特定的陣列僅顯示在最後的記錄10663.
請幫助
'$值 - > subscriptionContractId;'。循環時,$ key是索引(0,1,2,3等),$ value是Object,假設'$ getContract'是一個'array' http://sandbox.onlinephpfunctions.com/code/ f60187c3ca44f580d6118e662a95912f087b4fc7 – briosheje
您的預期輸出是什麼?值的數組? – Sadikhasan
另外,改變:'$ this-> data ['res'] = $ cancel;'to'$ this-> data ['res'] [] = $ cancel;'並定義'$ data ['res'] '作爲'數組',否則你會每次都會覆蓋它的值,這會導致你只能檢索到你檢索到的最後一個值。 – briosheje