【发布时间】:2015-09-24 00:34:11
【问题描述】:
我有一个嵌套数组,我想知道如何在其中查找元素。
这是数组:
$test = [
['item_id' => 780, 'quantity' => 1],
['item_id' => 759, 'quantity' => 3],
['item_id' => 453, 'quantity' => 12]
];
我确实设法使用了foreaech 循环,但想知道是否有更好的方法?
foreach($test as $t) {
if($t['item_id']==780) echo $t['quantity'];
}
【问题讨论】:
-
item_id在数组中是唯一的吗?如果是,您可以修改数据结构,使其以该值为键,然后数量查找将简化为数组索引。