Jump to content

Array within Array (for each)


George Botley

Recommended Posts

Hi,

 

 

Here's the array under the $img variable

 

 

Array ( [0] => Array ( [url] => IMG_0002.JPG ) ) 1 

 

It would normally have multiple values, depending on the amount the users adds to the form.

 

 

Basically, for key '0' on the array I wish to then get the value of the sub array "url".

 

 

How would I do this in a foreach loop?

 

George.

Link to comment
https://forums.phpfreaks.com/topic/256279-array-within-array-for-each/
Share on other sites

this will grab each entry in the array, and then you pull the named key (url) for that entry and do what you want, along with any other keys. it is best if your keys are always the same.

<?php
foreach ($array as $entry){
echo $entry['url']; // named array key in nested array
}
?>

 

or this will cycle through each entry, and then each value in the entry. it's cumbersome and not recommended unless you need to process each value THE SAME, separately.

 

<?php
foreach ($array as $entry){
foreach ($entry as info) {
// do something with each piece of info, in each entry
}
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.