$out = Array ( [orders] => Array ( [0] => Array ( [id] => 14138 [likes_all] => 35356 [likes_remain] => 11621 [item] => Array ( [item_id] => 368395724 [album] => profile [img_src] => https://pp.vk.me/c837331/v837331336/1bbef/c8lXmmfhlp4.jpg ) [user] => Array ( [uid] => 178515510 ) ) [1] => Array ( [id] => 79931 [likes_all] => 25753 [likes_remain] => 7475 [item] => Array ( [item_id] => 411939044 [album] => profile [img_src] => https://pp.vk.me/c837331/v837331336/1bbef/c8lXmmfhlp4.jpg ) [user] => Array ( [uid] => 178515510 ) ) ) ) 

How to output array elements through foreach?

  • Only minus are able to put - Pasha Talyanchuk
  • +)) Why do you need while? foreach fit? - ultimatum
  • Yes, only the task has changed a little bit - Pasha Talyanchuk
  • Change the question - ultimatum
  • Already changed .... - Pasha Talyanchuk

2 answers 2

If there is one 'orders' element in the main array:

 foreach ($out['orders'] as $order) { echo $order['likes_all'] . '<br>'; echo $order['likes_remain'] . '<br>'; echo $order['item']['img_src'] . '<br>'; } 

If the main array has many elements and 'orders' is only one of them:

 foreach ($out as $key => $value) { echo $key . ':<br>'; // 'orders:' foreach ($value as $el) echo $el['likes_all'] . '<br>'; echo $el['likes_remain'] . '<br>'; echo $el['item']['img_src'] . <br>; } } 

    If you understand correctly, you have an array in the array. In the first foreach you get access to [orders], in the second foreach you [orders] scroll through and get access to likes_all, likes_remain and in the last we get img_src.

    Documentation is available in Russian by reference . Try this:

     foreach ($out as $my_array) { foreach ($my_array['orders'] as $order) { echo $order['likes_all']; echo $order['likes_remain ']; foreach ($order['item'] as $item) { echo $item['img_src']; } } } 
    • Notice: Undefined index: orders - Pasha Talyanchuk
    • What do you have in the first array? Have [orders]? - ultimatum
    • Well, yes, since in the matter I have an array - Pasha Talyanchuk
    • I need likes_all, likes_remain and img_src - Pasha Talyanchuk
    • added readability to your question, is the structure correct? - ultimatum