Quantcast
Viewing all articles
Browse latest Browse all 11

Multidimensional Array!

Hi everyone!
Going back a little while, we began talking about Arrays. We discussed that an Array is a single list of values/keys.
Sometimes however, you might want to store more than one key in a value.
This is called a multidimensional array.

PHP is able to understand an array that is many levels deep. However, it’s difficult for most people to manage an array that’s more than three levels deep.

Let’s begin with a two dimensional array, which is basically an array of arrays:

Take a look at the following table, and then we will write it out using php.

Name Age City
Sarah 10 Toronto
Dina 17 Chicago
Esther 22 Detroit
Adina 26 Baltimore

Here is how you would write this out:

$girlss = array
(
array(“Sarah”,10,Toronto),
array(“Dina”,17,Chicago),
array(“Esther”,22,Detroit),
array(“Adina”,26,Baltimore)
);
Now the two-dimensional $girls array contains four arrays, and it has contains a row and a column.

We will now write the echo statements based on the above array:

“;
echo $girls[1][0].”: Age: “.$girls[1][1].”, City: “.$girls[1][2].”.
“;
echo $girls[2][0].”: Age: “.$girls[2][1].”, City: “.$girls[2][2].”.
“;
echo $girls[3][0].”: Age: “.$girls[3][1].”, City: “.$girls[3][2].”.
“;
?>

The output of this multidimensional array will look like this:

Sarah: Age: 10, City: Toronto.
Dina: Age: 17, City: Chicago.
Esther: Age: 22, City: Detroit.
Adina: Age: 26, City: Baltimore.

Good luck practicing multidimensional arrays!!


Viewing all articles
Browse latest Browse all 11

Trending Articles