➦
Back
🚀Show
<?php
//Single indexed array
$studentNames = array("Emma", "Logan", "Jordan", "Amy");
$studentGender = array("F","M","NA","F");
$studentAge = array(18,5,11,17);
//Multidimensional array
$multiStudents = array(array("Emma","F",18), array("Logan","M",5), array("Jordan","NA",11), array("Amy","F",17));
//Associative array
$logan = array("name"=>"Logan", "gender"=>"M", "age"=>5);
//MIX Multi + Associate
$multiAssociateStudents = array("Emma"=>array("gender"=>"F", "age"=>18),
"Logan"=>array("gender"=>"M", "age"=>5),
"Jordan"=>array("gender"=>"NA", "age"=>11),
"Amy"=>array("gender"=>"F", "age"=>17));
echo "<h1>SINGLE Dimension Array -----------------------------</h1>";
//"Logan M 5"
echo $studentNames[1]." ";
echo $studentGender[1]." ";
echo $studentAge[1];
echo "<h1>MULTI Dimensional Array -----------------------------</h1>";
//"Logan M 5"
echo $multiStudents[1][0]." ";
echo $multiStudents[1][1]." ";
echo $multiStudents[1][2];
echo "<h1>ASSOCIATIVE Array -----------------------------</h1>";
//"Logan M 5"
echo $logan["name"]." ";
echo $logan["gender"]." ";
echo $logan["age"];
echo "<h1>MIX MULTI + ASSOCIATIVE Array -----------------------------</h1>";
//Get the associate ID ("Logan") using index positions again lol
//In this case the array_keys are ("Emma", "Logan", "Jordan", "Amy") and we want the 2nd position which is "Logan"
$keys = array_keys($multiAssociateStudents);
//"Logan M 5"
echo $keys[1]." "; //or simply echo "Logan" because we know who we want
echo $multiAssociateStudents["Logan"]['gender']." ";
echo $multiAssociateStudents["Logan"]['age'];
?>