PHP Arrays


 

PHP Arrays

Array is a special variable that allows to store more than one value including the whole bunch of variable inside a variable.

Array is a special variable that allows to store more than one value including the whole bunch of variable inside a variable.

Array is a special variable that allows to store more than one value including the whole bunch of variable inside a variable. For example, if you have a list of items, which are to be stored in single variables, looks like:

$Company1=“Reliance”;
$Company2= “Tata”;
$Company3= “Birla”;

We can determine the variables like the above method but if the list of items is too big, then it will be difficult and time consuming to determine variable like above. For this, the best solution is to use an Array.

The above three values can be hold in a single array that can be accessed by referring to the array name.

Each element in the array has its own index so that it can be easily accessed.

There are three kinds of arrays in PHP:

  • Numeric array - An array with a numeric index
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

3.7.1. Numeric Arrays

A numeric array stores numeric data in each index. There are two methods of creating a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):
$Companies=array(“Reliance”,“Tata”, “Bajaj”, “Birla”);

2. In the following example we assign the index manually:
$companies[0]=“Reliance”;
$companies [1]=“Tata”;
$companies [2]=“Bajaj”;
$companies[3]=“Birla";

For example:

In the following example you access the variable values by referring to the array name and index:
<?php
$companies[0]=“Reliance”;
$companies [1]=“Tata”;
$companies [2]=“Bajaj”;
$companies[3]=“Birla";
echo $companies[0] . " and " . $companies[1] . “ are India’s biggest corporate groups.”;
?>       

The code above will output:
Reliance and Tata are India’s biggest corporate groups.

3.7.2. Associative Arrays

An associative array is an abstract data type made of a collection of unique keys and unique values associated with their respective key ID. It is basically used for string data type not numeric. In associative array, a pair of “=” and “>” is being used to denotes names and their assigned value inside the array() function.

The finding the values in associated array is called as lookup or indexing in which each key ID looks up their respective values. For Example: If the value associated with the key ‘Tata’ is 2, we can say that our array maps ‘Tata’ is 2.

It is advisable to store the specific named values in string because it is the best way to store the specific name into string. Thus, the main function of the associative arrays is to be used as memoriser with the help of keys ID and their assigned value.

Example 1

In this example we use an array to assign marks to the different students:
$marks = array(“Amit”=>40, “Vijay”=>28, “Kumar”=>44);

We can also write this example in different way like:
$marks[‘Amit’] = “40”;
$ marks[‘Vijay’] = “28”;
$ marks[‘Kumar’] = “44”;

In PHP we can write this example using ID keyslike this:
 <?php
$marks[‘Amit’] = “40”;
$ marks[‘Vijay’] = “28”;
$ marks[‘Kumar’] = “44”;

echo “Vijay got “ . $marks[‘Vijay’]. “marks”;
?>

The code above will output:
Vijay got 44 marks.

3.2.2. Multidimensional Arrays

Multidimensional array of dimension n is a collection of items that is accessed through n subscript expressions. Each element in the main array in a multidimensional array works as an array and each element in the sub-array can also be an array, and so on.

Example:

Here we are printing the numbers from 1 to 9 in 3*3 matrix.

<?php

$matrix = array(
            "row1"=> array("col1"=>"1","col2"=>"4","col3"=>"7"),
            "row2"=> array("col1"=>"2","col2"=>"5","col3"=>"8"),
            "row3"=> array("col1"=>"3","col2"=>"6","col3"=>"9"));

echo "Value at row1 and col2 =".$matrix['row1']['col2'];

0

echo "<br>Printing number through matrix <br>";

foreach($matrix as &$value){
            echo "<br>";
            foreach($value as &$val){
                        echo "  ".$val."  ";
            }
}

?>

1

Output

The output would be:

Value at row1 and col2 =4
Printing number through matrix

2

1 4 7
2 5 8
3 6 9

Ads