2015-07-01 52 views
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.

請幫助

+0

'$值 - > subscriptionContractId;'。循環時,$ key是索引(0,1,2,3等),$ value是Object,假設'$ getContract'是一個'array' http://sandbox.onlinephpfunctions.com/code/ f60187c3ca44f580d6118e662a95912f087b4fc7 – briosheje

+0

您的預期輸出是什麼?值的數組? – Sadikhasan

+0

另外,改變:'$ this-> data ['res'] = $ cancel;'to'$ this-> data ['res'] [] = $ cancel;'並定義'$ data ['res'] '作爲'數組',否則你會每次都會覆蓋它的值,這會導致你只能檢索到你檢索到的最後一個值。 – briosheje

回答

1

您可以修改代碼如下(見php documenation about foreach更多背景):

$cancel = array(); 
foreach($getContract as $value) { 
    $cancel[] = $value->subscriptionContractId;  
    ... 
} 

這樣,在每一次迭代中,$value都會從您的getContract數組中分配一個值,它的鍵被省略。然後,您可以將值subscriptionContractId存儲在您的$cancel陣列中,以執行您打算執行的任何操作。

注:briosheje是正確的進線:

$this->data['res'] = $cancel; 

$this->data['res']每次迭代覆蓋。當你用你的$cancel數組覆蓋它時,你的代碼仍然可以工作,但是你在這裏做了不必要的工作,因爲你的數組在下一次迭代時可能會受到進一步的改變。

我的建議是推動這一行你的循環結束支架,讓您的結果保存一次:

... 
foreach($getContract as $value) { 
    ... 
} 
$this->data['res'] = $cancel;