Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Joe Li
8,104 PointsAn Array inside An Array inside An array.
Hey Guys,
I'm trying to do a foreach loop inside of a foreach loop, therefore I need a multi-dimension array, 4 levels deep array.
$projects = array();
$projects[1] = array(
"name" => "Project 1",
"location" => "Location One",
"link" => "project.php",
"main-image" => "img/projects/thumbs/branding1.jpg",
"headImage" => "bg_project1",
"category" => "Kitchen",
"description" => " A nice kitchen with lot of lighting.",
"images" => array(
"img/projects/thumbs/branding1.jpg",
"img/projects/thumbs/web1.jpg",
"img/projects/thumbs/web2.jpg")
);
I know how I would access the images for a foreach loop int he example above.
But what if I wanted to do an array of the images, e.g. give the image a name, or location etc. How do I do an array of these images?
"images" => array(
"img/projects/thumbs/branding1.jpg",
"img/projects/thumbs/web1.jpg",
"img/projects/thumbs/web2.jpg")
How would i do this? and how would I call it?
Thanks,
Joe
3 Answers
James Warner
2,322 PointsHey Joe,
You nearly had it! here's how I would add image information and loop through it nicely:
<?php
$projects = array();
// Add a project
$projects[] = array(
'name' => 'Project 1',
'description' => 'Example Description',
'images' => array(
array(
'url' => 'img/projects/thumbs/branding1.jpg',
'name' => 'Branding Image'
),
array(
'url' => 'img/projects/thumbs/web1.jpg',
'name' => 'Web Image 1'
),
array(
'url' => 'img/projects/thumbs/web2.jpg',
'name' => 'Web Image 2'
)
)
);
// Now let's loop through the projects
foreach ($projects as $project) {
// We can access the project values here
echo $project['name'];
echo $project['description'];
// Time for the images
foreach ($project['images'] as $image) {
// We can access the image values here
echo $image['url'];
echo $image['name'];
}
}
Paul Yabsley
46,713 PointsIt's like you've done but you would need a key for each image:
$projects[1] = array(
"name" => "Project 1",
"location" => "Location One",
"link" => "project.php",
"main-image" => "img/projects/thumbs/branding1.jpg",
"headImage" => "bg_project1",
"category" => "Kitchen",
"description" => " A nice kitchen with lot of lighting.",
"images" => array(
"image1" => array(
"img/projects/thumbs/branding.jpg",
"Location",
"Name"
),
"image2" => array(
"img/projects/thumbs/web1.jpg",
"Location of image 2",
"Name"
),
"image3" => array(
"img/projects/thumbs/web2.jpg",
"Location",
"Name"
)
)
);
// They can be accessed like this
$foo = $projects[1]["images"]["image2"][1];
echo $foo;
Navid Mirzaie Milani
6,274 Pointssoo what i understand of your post you want to make each image an array like
$img1 = array(
name => "bla.jpg", propertie2 => "bla2", propertie3 => 'bla3'
);
like that? or do you mean something else, if you more specify your question i can help you better.
Joe Li
8,104 PointsJoe Li
8,104 PointsThanks James you nailed it!