I have an array that outputs like this:
我有一个输出像这样的数组:
1 =>
array
'quantity' => string '2' (length=1)
'total' => string '187.90' (length=6)
2 =>
array
'quantity' => string '2' (length=1)
'total' => string '2,349.90' (length=8)
I would like to loop through each array keys and retrieve the set of 3 values relating to them, something like this (which doesnt work):
我想循环遍历每个数组键并检索与它们相关的3个值的集合,类似这样(这不起作用):
foreach( $orderItems as $obj=>$quantity=>$total)
{
echo $obj;
echo $quantity;
echo $total;
}
Would someone be able to give some advice on how I would accomplish this, or even a better way for me to be going about this task. Any information relating to this, including links to tutorials that may cover this, would be greatly appreciated. Thanks!!
有人能够就如何实现这一目标提出一些建议,甚至是一个更好的方式让我去完成这项任务。任何与此相关的信息,包括可能涵盖此内容的教程链接,都将不胜感激。谢谢!!
2 个解决方案
#1
5
foreach( $orderItems as $key => $obj)
{
echo $key;
echo $obj['quantity'];
echo $obj['total'];
}
Using the above.
使用上面的。
#2
2
You need to read the docs on forEach()
a little more, since your syntax and understanding of it is somewhat incorrect.
您需要更多地阅读forEach()上的文档,因为您的语法和对它的理解有些不正确。
$arr = array(
array('foo' => 'bar', 'foo2', 'bar2'),
array('foo' => 'bar', 'foo2', 'bar2'),
);
foreach($arr as $sub_array) {
echo $sub_array['foo'];
echo $sub_array['bar'];
}
forEach()
iteratively passes each key of the array to a variable - in the above case, $sub_array
(a suitable name, since your array contains sub-arrays). So within the loop body, it's that you need to interrogate.
forEach()迭代地将数组的每个键传递给变量 - 在上面的例子中,$ sub_array(一个合适的名称,因为你的数组包含子数组)。所以在循环体内,你需要询问。
#1
5
foreach( $orderItems as $key => $obj)
{
echo $key;
echo $obj['quantity'];
echo $obj['total'];
}
Using the above.
使用上面的。
#2
2
You need to read the docs on forEach()
a little more, since your syntax and understanding of it is somewhat incorrect.
您需要更多地阅读forEach()上的文档,因为您的语法和对它的理解有些不正确。
$arr = array(
array('foo' => 'bar', 'foo2', 'bar2'),
array('foo' => 'bar', 'foo2', 'bar2'),
);
foreach($arr as $sub_array) {
echo $sub_array['foo'];
echo $sub_array['bar'];
}
forEach()
iteratively passes each key of the array to a variable - in the above case, $sub_array
(a suitable name, since your array contains sub-arrays). So within the loop body, it's that you need to interrogate.
forEach()迭代地将数组的每个键传递给变量 - 在上面的例子中,$ sub_array(一个合适的名称,因为你的数组包含子数组)。所以在循环体内,你需要询问。