pTutorial

New way to learn PHP

PHP 5 Multidimensional array

Multidimensional arrays

In the previous chapter, I explained arrays that are a single list of key and value pairs. However, on sometimes, you may want to store more than one value and key. This type of array is called multidimensional array or array of array.

Example of multidimensional array

<?php

$info = array(

array(
"name" => "john",
"age" => "23",
"c_year" => 2014
),

array(
"name1" => "maclen",
"age" => "25",
"c_year" => 2014
),

);

print_r ( $info );

?>

Creating multidimensional array

You can create multidimensional array same an array.

$foodPrices['cloth']['jeans'] = 1500;
$foodPrices['cloth']['tshit'] = 1300;

You can assign key as number like that:-

color[light][0] = Red;
color[light][1] = Blue;

Another way to create multidimensional array:-

color[light][0] = Red;
color[light][1] = Blue;
array(
"name" = > "john",
"age" = > "23",
"c_year" = > 2014
),
array(
"name" = > "maclen",
"age" = > "25",
"c_year" = > 2014
),
);

Access element of multidimensional array

<?php

$info = array(

array(
"name" => "john",
"age" => "23",
"c_year" => 2014
),

array(
"name1" => "maclen",
"age" => "25",
"c_year" => 2014
),

);

$Num=0;
foreach ( $info as $details ) {
$Num++;
echo "

Book #$Num:

"; echo "
"; foreach ( $details as $key => $value ) { echo "
$key
$value
"; } echo "
"; } ?>

QUOTES OF THE DAY

Knowledge itself is a power.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER