Multidimensional Arrays in PHP: syntax and example

Multidimensional Arrays in PHP:
In previous post Arrays in PHP we learned Arrays and types of arrays.
We understood the concept of regular array that is indexed array, it is also called numeric array. We understood associative array.

Now we are going to understand Multidimensional array.
A multidimensional array is an array containing one or more arrays. It is also called array of array.

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.

Two-dimensional Arrays:
A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).




example:
<?php
$students=array(
          array('Vanesh', 'MCA', '78'),
          array('Ganesh', 'MCM', '83'),
          array('Ramesh', 'MSC', '64'),
);

echo $students[0][0]." ".$students[0][1]." ".$students[0][2]. "<br/>";
echo $students[1][0]." ".$students[1][1]." ".$students[1][2]. "<br/>";
echo $students[2][0]." ".$students[2][1]." ".$students[2][2]. "<br/>";

?>


output:
Vanesh MCA 78
Ganesh MCM 83
Ramesh MSC 64

We can print this array using foreach loop as:

<?php
$students=array(
          array('Vanesh', 'MCA', '78'),
          array('Ganesh', 'MCM', '83'),
          array('Ramesh', 'MSC', '64'),
);
foreach($students as $key => $val){
  foreach($val as $v){
      echo $v." ";
    }
  echo "<br/>";
}
?>

output:
Vanesh MCA 78
Ganesh MCM 83
Ramesh MSC 64

Arrays in PHP

Arrays in PHP:
An array stores multiple values in one single variable.




An array is a data structure that stores one or more similar type of values in a single value.

Types of array:

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.

Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

Numeric Array
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Example
Following is the example showing how to create and access numeric arrays.

Here we have used array() function to create array. In PHP, the array() function is used to create an array.

<?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>

Output:
Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five 

Associative Arrays:
Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

or:

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
The named keys can then be used in a script:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

OUTPUT:
Peter is 35 years old.

Loop Through an Associative Array
To loop through and print all the values of an associative array, you could use a foreach loop, like this:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

OUTPUT:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43




Multidimensional Arrays:

Multidimensional Arrays will learn in next lession. 

For loop in PHP : Introduction with Example

The PHP for Loop:



The for loop is used when you know how many times you want to execute a statement or a block of statements.

Today we are going to learn for loop in php.

Syntax:

for (init counter; test counter; increment counter) {
    code to be executed;
}

Parameters:

init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value

The example below displays the numbers from 0 to 10:

Example
<?php 
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";

?>

OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10