PHP Array behaves like special variables that able to hold more than one value.

PHP Array :

By definition, PHP arrays are a group of similar quantities that are stored within adjacent ‘cells’ that are accessible as one, using a single variable.

We visited the PHP array briefly when we covered the topic on data types. We defined them as “Arrays are variables that are used to store many values.”

Syntax :

$var_name=array(“Parameter1”, “parameter2”, “Parameter3”);

PHP Array Example :

<?php
$phones = array("iPhone 7S", "Galaxy S7", "Huawei Ascend P8");
echo "The phones that are currently trending include " . $phones[0] . ", " . $phones[1] . ", and " . $phones[2] . ".";
?>

Output :

The phones that are currently trending include iPhone 7S, Galaxy S7, and Huawei Ascend P8.

In the above example, the variable $phones is used to store information about trending phones. It holds strings “iPhone 7S”,”Galaxy S7″, and “Huawei Ascend P8”.

Arrays help to avoid duplicating variables for similar data groups. It is easy to create three variables for the example above. However, that becomes complicated when referring to a large number of data sets for similar data, say 3000 or more.

The basic syntax for an array is by assigning a variable to the data set using the array(); function as shown in the above syntax.

Types of PHP Array :

PHP Arrays are divided into three categories:

  • Associative array
  • Indexed arrays
  • Multidimensional arrays

PHP Associative Array :

Associative arrays are named with keys. One can either state has been used below :

<?php

$marks = array("English"=>"91", "Math"=>"87", "Geography"=>"68","Science"=>"99");
?>

Or

<?php

$marks['English'] = "91";
$marks['Math'] = "87";
$marks['Geography'] = "68";
$marks['Science'] = "99";
echo "Cara got ".$marks['English']." in English and " .$marks['Science']." in Science.</br>";
?>

Output:

Cara got 91 in English and 99 in Science.

PHP Associative Array Looping :

All members of an Array can be printed using the foreach loop, in a manner as used in the following example.

<?php

$marks['English']="91";
$marks['Math']="87";
$marks['Geography']="68";
$marks['Science']="99";
echo "Cara got ".$marks['English']." in English and " .$marks['Science']." in Science.</br>";
echo "<b>This is the result of using the foreach loop on Arrays</b></br>";
foreach($marks as $x=>$x_value){
echo "Subject=".$x.", Marks=".$x_value."</br>";
}
?>

Output:

Cara got 91 in English and 99 in Science.
 This is the result of using the foreach loop on Arrays
Subject=English, Marks=91
Subject=Math, Marks=87
Subject=Geography, Marks=68
Subject=Science, Marks=99

PHP Indexed Arrays :

PHP index arrays are shored the values on index basis. Indexed arrays may be created in the following ways.

<?php

$marks[0]="English";
$marks[1]="Math";
$marks[2]="Geography";
$marks[3]="Science";
echo "Cara loves to study. ".$marks[0]." and " .$marks[1]." are her favorite subjects</br>";
?>

Output:

Cara loves to study. English and Math are her favorite subjects

An array size may be counted using the count() function.

PHP array count :

In the following example, we count the number of values contained within Cara’s marks above.

<?php

$marks[0]="English";
$marks[1]="Math";
$marks[2]="Geography";
$marks[3]="Science";
echo "Cara loves to study. ".$marks[0]." and " .$marks[1]." are her favorite subjects</br>";
echo "She studies a total of ".count ($marks). " subjects.";
?>

Output:

Cara loves to study. English and Math are her favorite subjects
She studies a total of 4 subjects.

PHP indexed Arrays Looping :

Looping may be done using the following method:

<?php

$marks[0]="English";
$marks[1]="Math";
$marks[2]="Geography";
$marks[3]="Science";
echo "Cara loves to study. ".$marks[0]." and " .$marks[1]." are her favorite subjects</br>";
echo "She studies a total of ".count ($marks). " subjects. They are: </br>";
$length=count ($marks);
for($i=0;$i<$length;$i++){
sort ($marks);
echo $marks[$i];
echo "</br>";

}
?>

Output:

Cara loves to study. English and Math are her favorite subjects
She studies a total of 4 subjects. They are:
English
Geography
Math
Science

PHP Multidimensional arrays :

Multidimensional arrays are those that contain more than one data set. They will be discussed here..

Happy Learning 🙂