2010-04-09 55 views
0

我有這個數組:如何訪問此多維數組內的數據?

$items_pool = Array ( 
[0] => Array ([id] => 1 [quantity] => 1) 
[1] => Array ([id] => 2 [quantity] => 1) 
[2] => Array ([id] => 72 [quantity] => 6) 
[3] => Array ([id] => 4 [quantity] => 1) 
[4] => Array ([id] => 5 [quantity] => 1) 
[5] => Array ([id] => 7 [quantity] => 1) 
[6] => Array ([id] => 8 [quantity] => 1) 
[7] => Array ([id] => 9 [quantity] => 1) 
[8] => Array ([id] => 19 [quantity] => 1) 
[9] => Array ([id] => 20 [quantity] => 1) 
[10] => Array ([id] => 22 [quantity] => 1) 
[11] => Array ([id] => 29 [quantity] => 0) 
) 

我通過這個數組試圖環和執行基於$items_pool[][id]的價值條件。然後我想要返回TRUE或NULL/FALSE,所以我只是測試具體的存在。

+1

有點不清楚你想才達到什麼......請舉一個例子... – 2010-04-09 22:30:12

+0

*(參考)* http://de2.php.net/manual/en/language.types.array.php,http://de2.php.net/manual/en/language.control-structures.php – Gordon 2010-04-09 22:34:36

+1

http://stackoverflow.com/questions/2611040/how-do-i-make-these-fields-autopopulate-from-the-database/2611449#2611449 - grrr;生氣的樣子。 – bigstylee 2010-04-09 23:02:39

回答

2

事情是這樣的:

$items_pool = array(...); 
$result = false; 

foreach ($items_pool as $item) { 
    if ('something' == $item['id']) { 
     $result = true; 
     break; 
    } 
} 
+1

可能值得在'break'中加入'break';'$ result = true;'後停止不必要的循環 – 2012-01-05 11:53:48

+0

@TheArtfulBenny:'break'實際上是這種情況下的一個要求。感謝您的通知,修復。 – Crozin 2012-01-05 13:26:36

1

循環通過檢查如果有什麼是空..

foreach($items_pool as $arr){ 
    echo $arr['id'].'==>'.$arr['quantity']; 
    if($arr['quantity'] == 0){ 
     echo 'id:'.$arr['id'].' is empty!'; 
     return false; 
    } 
}