2013-07-12 59 views
-2

我最近想出了一個相當討厭的問題。一個陣列內的元素相結合給我留下了雙陣列:結合PHP數組出條件和foreach循環

$w_invoices = array(); 
if($w_invoices_get['numreturned'] == '1') 
{ 
    $w_invoices[] = $w_invoices_get['invoices']['invoice']['id']; 
// Just put $w_invoices_get array element into $w_invoices array 
} 
else 
{ 
    foreach($w_invoices_get['invoices']['invoice'] as $key=>$w_invoice_id) 
    { 
    $w_invoices[] = $w_invoice_id['id']; 
    // Acquire array's array element into $w_invoices array 
    } 
} 

print_r顯示: Array ([0] => 1 [1] => 2) Array ([0] => 4)但我想這是一個數組

+0

如果你不解釋$ w_invoces_get – edi9999

+0

的結構,我認爲我們不能提供幫助。你能重建一個更清晰的結構嗎!有些數據是模糊的,只是萌芽。 –

+0

執行'$ w_invoices_get ['invoices'] ['invoice'] ['id']'和'$ w_invoice_id ['id']''var_dump''。它看起來像其中一個是一個數組。 –

回答

1

也許嘗試:

else { 
    for (myCount = 0; myCount < $w_invoices_get['numreturned']; myCount++) { 
     $w_invoices[myCount] = $w_invoices_get['invoices']['invoices']['id'][myCount]; 
    } 
} 

我的猜測是$w_invoices_get['invoices']['invoices']不是一個數組,而$w_invoices_get['invoices']['invoices']['id']是,但是我不能判斷這個沒有看到代碼本身。

+0

猜測應該在評論中。所以儘快獲得50個代表:) – HamZa

+0

是的,你是對的。第二個是數組。 – jakubplus

+0

那麼你是''w_invoices_get ['invoices'] ['invoices'] ['id']'給你的數組賦值,你剛剛確認是一個數組,你應該期望一個嵌套數組。 – Tro