0

I have the following multidimensional array:

Array 
( 
  [0] => 57950340 
  [1] => SALE-86 
  [2] => COMPLETE 
  [3] => 
  [4] => 333 
  [5] => 819 
  [6] => Array 
         ( 
         [0] => Array 
                ( 
                 [number] => 1 
                 [product] => Array
                              ( 
                               [id] => 90316 
                               [name] => CLASSIC COCKTAIL 
                              ) 
                [quantity] => 1 
                [price_variation] => 1 
                [modifiers] => Array( )
                [notes] => 
                [unit_price] => 16.3636 
                [unit_tax] => 1.63636 
               ) 
        [1] => Array 
               ( 
                [number] => 2 
                [product] => Array 
                             ( 
                              [id] => 90316 
                              [name] => CLASSIC COCKTAIL 
                             ) 
                [quantity] => 1 
                [price_variation] => 1 
                [modifiers] => Array ( ) 
                [notes] => 
                [unit_price] => 16.3636 
                [unit_tax] => 1.63636 
               ) 
         )
)

I'm trying to loop through the array so that I can echo the name of the product items (held within the array at key 6 and echo each of these out on a separate line with the unit price and an the initial order ID (key 0 of the initial array).

I've been trying to do this for a few hours but am going round in very confusing circles, can anyone shed any light on how I can do this?

2 Answers 2

1
<?PHP
  $multi_dimensional_array = [...]; // Your array here

  $order_id = $multi_dimensional_array[0];
  $products_array = $multi_dimensional_array[6];
  foreach($products_array as $product) {
    echo $product['product']['name']." costs ".$product['unit_price'];
    echo " - ORDER: ".$order_id;
    echo "<br/>";
  }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Useforeach and is_array() to check if the values is array then foreach to access the inside variables then lastly you can echo it.

 foreach($array as $key => $val)
 {
   if(is_array($val){
     foreach($val as $key2 => $val2)
     {
       //print_r($val2); to see the actual data you are accessing
       echo "ID: " . $val2['product']['id']. ' Product Name: ' . $val2['product']['name'] . ' Quantity: ' . $val2['quantity'];
     }
   }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.