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
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
}
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.