English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to store multiple values in a single variable in PHP.
Arrays are complex variables that allow us to store multiple values or a group of values under a single variable name. Suppose you want to store colors in a PHP script, storing colors one by one might look like this:
<?php $color1 = "Red"; $color2 = "Green"; $color3 = "Blue"; echo $color1; echo "<br>"; echo $color2; echo "<br>"; echo $color3; ?>Test and see‹/›
But if you want to store the country/If the state name or city name of a region is stored in a variable, then this time it may not be just three but a hundred. Storing the name of each city in a separate variable is a difficult, bad, and impractical approach. Arrays come into play here.
You can create three types of arrays. These are:
Indexed array — An array with numeric keys.
Associative array — An array where each key has a specific value.
Multidimensional Array — An array that itself contains one or more arrays.
Indexed arrays or numeric arrays store each array element that has a numeric index. The following example shows two methods to create an indexed array, the simplest method being:
<?php //Define an indexed array $colors = array("Red", "Green", "Blue"); // Print array structure print_r($colors); ?>Test and see‹/›
Note:In indexed or numeric arrays, indices are automatically assigned starting from 0, and values can be of any data type.
This is equivalent to the following example, where indices are manually assigned:
<?php $colors[0] = "Red"; $colors[1] = "Green"; $colors[2] = "Blue"; // Print array structure print_r($colors); ?>Test and see‹/›
In an associative array, the key assigned to the value can be arbitrary and user-defined strings. In the following example, the array uses keys instead of index numbers:
<?php //Define an associative array $ages = array("Peter"=>22, "Clark"=>32, "John"=>28); // Print array structure print_r($ages); ?>Test and see‹/›
The following example is equivalent to the previous one, but shows another way to create an associative array:
<?php $ages["Peter"] = ""22"; $ages["Clark"] = ""32"; $ages["John"] = ""28"; // Print array structure print_r($ages); ?>Test and see‹/›
A multidimensional array is an array where each element can also be an array, and each element in the sub-array can be an array, or further contain arrays within it, and so on. An example of a multidimensional array is shown below:
<?php //Define a multidimensional array $contacts = array( array( "name" => "Peter Parker", "email" => "[email protected]", ), array( "name" => "Clark Kent", "email" => "[email protected]", ), array( "name" => "Harry Potter", "email" => "[email protected]", ) ); //Access nested values echo "Peter Parker's email id is: " . $contacts[0]["email"]; ?>Test and see‹/›
You can use either of the two statements var_dump() or print_r() to view the structure and values of any array. However, the print_r() statement provides less information. See the following example:
<?php //Define the array $cities = array("London", "Paris", "New York"); //Display the city array print_r($cities); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => London [1] => Paris [2] => New York )
This output shows the keys and values of each element in the array. To get more information, use the following statement:
<?php //Define the array $cities = array("London", "Paris", "New York"); //Display the city array var_dump($cities); ?>Test and see‹/›
The var_dump() statement provides the following output:
array(3) { [0]=> string(6) "London" [1]5) "Paris" [2]8) "New York" }
In addition to keys and values, this output also displays the data type of each element, such as6a string of characters. Inthe next chapter,In the next chapter, you will learn how to sort array elements.
InLooping ChapterIn this section, you will learn how to iterate over the values of an array.