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
Lidija Novakovic
6,634 Points3Dimensional array?
Greetings to all, I am trying on my own to learn more about arrays but I can not solve this. My guess is that I need to combine a for loop with a foreach? Take a look at this example-
<?php
$books = array();
$books[0] = array(
"title" => "Greatest book title ever",
"ISBN" => 1323443228,
"pages" => 243,
"format" => array("PDF", "EBUB"),
);
$books[1] = array(
"title" => "Greatest book title ever",
"ISBN" => 1323443228,
"pages" => 243,
"format" => array("PDF", "EBUB"),
);
$books[2] = array(
"title" => "Greatest book title ever",
"ISBN" => 1323443228,
"pages" => 243,
"format" => array("PDF", "EBUB"),
);
$books[3] = array(
"title" => "Greatest book title ever",
"ISBN" => 1323443228,
"pages" => 243,
"format" => array("PDF", "EBUB"),
);
$books[4] = array(
"title" => "Greatest book title ever",
"ISBN" => 1323443228,
"pages" => 243,
"format" => array("PDF", "EBUB"),
);
//echo "<pre>";
//echo print_r($books);
//echo "</pre>";
foreach ($books as $book ) {
foreach ($book as $key => $value) {
echo "<div><span>{$key} :</span><span>{$value}</span></div>";
}
}
?>
How do I get the $books["format"] $key to display the values?
1 Answer
David Kaneshiro Jr.
29,247 PointsThis should work, but only if the array stored under the "format" key will always have 2 items.
foreach ($books as $book) {
foreach($book as $key => $value) {
if ($key == "format") {
echo "<div><span>{$key} :</span><span>{$value[0], $value[1]}</span></div>";
} else {
echo "<div><span>{$key} :</span><span>{$value}</span></div>";
}
}
}
If the array is going to have a varying amount of items then I'd do something like.
foreach ($books as $book) {
foreach($book as $key => $value) {
// Check if the $key is equal to "format" we will need to handle the output
// for this key differently from the other keys.
if ($key == "format") {
$temp_items = ""; // Temporary string that will hold the items in the "format" array.
// This foreach loop will go through each item in the "format" array.
foreach ($value as $item) {
$temp_items = $temp_items . $item . ", "; // Append the each item to the $temp_items string.
}
$temp_items = rtrim($temp_items, ", "); // Trim off the last ", " at the end of the $temp_items string.
echo "<div><span>{$key} :</span><span>{" . $temp_items . "}</span></div>";
} else {
echo "<div><span>{$key} :</span><span>{$value}</span></div>";
}
}
}
Lidija Novakovic
6,634 PointsLidija Novakovic
6,634 PointsDavid you just have advanced to status "godlike" =) Using if/else condition was the last thing on my mind. Excellent solution. I myself got tangled in this - foreach ($books as $book) {
And at the end I strangled myself!