PHP - 2D arrays - Quoting through array keys and extracting their values?

I have an array that is output as follows:

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 skip all the keys of the array and get a set of 3 values ​​related to them, something like this (which doesn't work):

foreach( $orderItems as $obj=>$quantity=>$total)
{
    echo $obj;
    echo $quantity;
    echo $total;
}

Someone will be able to give some advice on how I accomplished this, or even the best way for me in this solution. Any information related to this, including links to tutorials that may cover this, would be greatly appreciated. Thank!!

+5
source share
2 answers
foreach( $orderItems as $key => $obj)
{
    echo $key;
    echo $obj['quantity'];
    echo $obj['total'];
}

Using the above.

+5
source

You need to read the documents a forEach()little more, since your syntax and understanding of this is somewhat incorrect.

$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() - $sub_array ( , ). , , .

+2

All Articles