i

PHP Tutorial

PHP Arrays

Arrays

PHP arrays are a data structure that helps us to store multiple values inside a single variable. If you have many elements that belong to the same type, then you can create an array variable and store all the values in a single variable using arrays. If we have five values, it is easy to create five variables and print the value, but when we have 100 or 300 values of string data type, and we need to store or print them, it is very difficult to create 100 or 300 variables. Instead of creating so many variables, we can use the array to store all the elements using a single variable and access them using an index. 

Arrays in php are created using array() function, and there are three types of arrays in php.

  • Indexed Arrays

  • Associative Arrays

  • Multidimensional Arrays

>Indexed Arrays

Indexed arrays are also known as numeric arrays because the values are stored with an index in these arrays. In Numeric arrays, values are stored and accessed in a linear fashion. It can be created in two ways as below.

$colors = array(“Red”, “Black”, “Blue”, “Green”);

In the above way, the index is created automatically and starts from 0.

$colors[0] = “Red”;

$colors[1] = “Black”;

$colors[2] = “Blue”;

$colors[3] = “Green”;

In the second method as above, you can assign the index manually.

>Associative Arrays

Associative arrays use string index and each value in the array can be assigned a key instead of linear storage. So, in these types of arrays, you need to assign a named key to the values. You can create associative arrays in below two ways.

$salary = array( “Jack” =>2500 , “John” =>4500, “Paul” =>3200);

or 

$salary[‘Jack’] = 2500;

$salary[‘John’] = 4500;

$salary[‘Paul’] = 3200;

You can access associative arrays as below.

echo “Salary of John is “. $salary[‘John’]. “
”;

The output of the above statement will be – Salary of John is 4500.

>Multidimensional Arrays

Multidimensional arrays can store an array at its index or an index to another sub-array also, i.e. we can create arrays of arrays. So, each element of an array can store an array and the sub-array also can store an array in multidimensional arrays. To access these arrays, we use multiple dimensions, and you can create two, three, four, five, or more level arrays also in php, but it becomes complex as you increase the levels.

Two-dimensional arrays in php are explained in the below example.

If you want to save personal details of a person like a phone number and city, you can use multidimensional arrays.

           $PersonalDetails = array (

                      “Joe” => array(

                                   “mobile” => 0678543627;

                                   “City” => “London”;

                       ),

                       “Sana” =>array(

                                   “mobile” =>8784683524;

                                   “City” => “India”;

                       ),

                       “Paul” => array(

                                   “mobile” => 0235783569;

                                   “City” => “Mexico”;

                       )

           );

//You can view the data using for and foreach loop in a nested way.

$values = array_keys($PersonalDetails); 

for($x = 0; $x < count($PersonalDetails); $x++) 

    echo $values[$x] . "\n"; 

    foreach($PersonalDetails [$values[$x]] as $key => $value) { 

        echo $key . " : " . $value . "\n"; 

    } 

    echo "\n"; 

}

?> 

Output of the below code will be as below:

Joe

Mobile : 0678543627 

City: London 

Sana

Mobile : 8784683524

City : India

Paul

Mobile : 0235783569

City : Mexico