Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created September 11, 2013 21:27
Show Gist options
  • Save phpfiddle/6530012 to your computer and use it in GitHub Desktop.
Save phpfiddle/6530012 to your computer and use it in GitHub Desktop.
[ Posted by Justin ] Array Example - Multidimensional
<?php
//Made the headers bold on this one. Maybe can use css to do that next time?
$cars=array("Dodge"=>array("Avenger","Challenger","Charger","Dart"),"Toyota"=>array("Highlander","Tundra","Corolla"),"Nissan"=>array("Sentra","Altima","Maxima"));
foreach($cars as $x=>$x_value)
{
echo "<b>Make: $x<br></b>";
foreach($x_value as $y){
echo "$y<br>";
}
echo "<br>";
}
?>
<?php
//This code echos only models made by Toyota
echo "<br>";
echo "<br>";
echo "<br>";
echo "Make: Toyota";
echo "<br><br>";
$carString = '';
foreach($cars['Toyota'] as $x)
{
$carString .= $x.', ';
}
echo rtrim($carString, ' ,'), ".";
//URL FOR HELP ON THIS - http://www.developerdrive.com/2012/01/php-arrays-array-functions-and-multidimensional-arrays/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment