A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Example
In this example we create a two dimensional array to store marks of three students in three subjects:
This example is an associative array, you can create numeric array in the same fashion.
<html>
<body>
<?php
$marks = array(
"mohammad" => array
(
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"qadir" => array
(
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"zara" => array
(
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
</body>
</html>

This will produce following result:
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

Counting multidimensional arrays

We touched briefly on multidimensional arrays in Creating Arrays in PHP Section. Essentially, a multidimensional array is an array whose elements are also arrays.
By default, count() only counts the elements in the top level of a multidimensional array. Here’s an example:
<?php

$movieInfo = array( "directors" => array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" ),
"movies" => array( "Rear Window", "2001", "Taxi Driver", "Metropolis" ) );

// Displays "2"
echo count( $movieInfo );

?>

The above code only counts the 2 elements in the top-level array ("directors" and "movies").
If you want to count all the elements in a multidimensional array — that is, not just the top-level elements, but the elements in all arrays inside the array — then pass the constant COUNT_RECURSIVE as a second argument to count():
<?php

$movieInfo = array( "directors" => array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" ),
"movies" => array( "Rear Window", "2001", "Taxi Driver", "Metropolis" ) );

// Displays "10"
echo count( $movieInfo, COUNT_RECURSIVE );

?>

In the above example, count() first counts the 2 elements in the top-level array ("directors" and "movies"). Then it counts all 8 elements in the nested arrays ("Alfred Hitchcock" to "Fritz Lang", then "Rear Window"to "Metropolis"). This results in a grand total of 10 elements.

1 comments:

  1. Nice site....Please refer this site also Our vision success!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Real time methodologies
    Dot Net Course in Chennai | Best Dot Net Training Institute in Chennai
    Testing Courses in Chennai | Best Software Testing Training Institute in Chennai With Placement
    Java Training in Chennai | Core Java Training in Chennai | java course and certification
    PHP training in chennai | Best PHP Training in chennai

    ReplyDelete

 
Top
Blogger Template