2010-04-09 26 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; 
    } 
}