PHP arrays
In this part of the PHP programming tutorial, we will cover arrays. We will initialize arrays and read data from them.
Arrays are collections of data. A variable can hold only one item at a time. Arrays can hold multiple items. Most programming languages have various types of collections. Lists, maps, arrays, queues, stacks, tuples etc. PHP has a different approach. It has one type of collection, an array. Using different syntax, an array in PHP will behave like a list, an array, or a map.
Technically, a PHP array is a map. It is also called an associative array. A map associates values to keys.
Arrays are used to store data of our applications. There are three distinct things we do with arrays. First, we initialize them with application data. Next, we modify data using assignments or PHP array functions. We have quite a few functions that work with arrays. They enable us to modify, sort, merge, slice, shuffle the data inside the arrays. There are specific database handling functions for populating arrays from database queries. Several other functions return arrays. Finally, we display data to the console, or in web applications, we display data to the browser.
Initializing arrays
There are several ways, how we can initialize an array in PHP. We use the
array()
function to create an array. In its simplest form, the function takes an arbitrary number of comma separated values.<?php $names = array("Jane", "Lucy", "Timea", "Beky", "Lenka"); print_r($names); ?>
Here we create a $names array. It stores 5 female names. The
print_r()
function prints a human readable information about the variable.$ php init1.php Array ( [0] => Jane [1] => Lucy [2] => Timea [3] => Beky [4] => Lenka )
From the output we can see the names and their indexes by which we can access them.
<?php $continents[0] = "America"; $continents[1] = "Africa"; $continents[2] = "Europe"; $continents[3] = "Asia"; $continents[4] = "Antarctica"; $continents[5] = "Australia"; print_r($continents); ?>
This is another way of initializing an array in PHP. We create the
$continents
array by assigning values to array indexes. "America" has index 0, "Europe" has index 2 etc.<?php $continents = array( 1 => "America", 2 => "Africa", 3 => "America", 4 => "Asia", 5 => "Antarctica"); print_r($continents); ?>
In this example, we create the
$continents
array with the indexes specified. By default, the first index is zero. In our case, we start with 1.$ php init22.php Array ( [1] => America [2] => Africa [3] => America [4] => Asia [5] => Antarctica )
Now we have an array of continents with indexes that we have chosen.
<?php $languages[10] = "PHP"; $languages[20] = "Python"; $languages[30] = "Ruby"; $languages[40] = "PERL"; $languages[50] = "Java"; print_r($languages); ?>
The indexes do not have to be consecutive numbers. In our example, we have chosen numbers 10, 20, 30, 40, and 50 to be the indexes for the
$languages
array.<?php $actors[] = "Philip Seymour Hoffman"; $actors[] = "Tom Cruise"; $actors[] = "Bill Paxton"; $actors[] = "Adrien Brody"; $actors[] = "Danie Craig"; print_r($actors); ?>
When we do assignment initialization of a PHP array, we can omit the indexes. The PHP will automatically create the indexes for us.
$ php init4.php Array ( [0] => Philip Seymour Hoffman [1] => Tom Cruise [2] => Bill Paxton [3] => Adrien Brody [4] => Danie Craig )
The PHP interpreter has created consecutive indexes starting from zero.
<?php $novels[10] = "Doctor Zhivago"; $novels[11] = "War and Peace"; $novels[] = "In Cold Blood"; $novels[20] = "Crime and Punishment"; $novels[] = "Catch XII"; print_r($novels); ?>
In this script, we have omitted two indexes. The PHP will add them. It will create index 12 and index 21.
$ php init5.php Array ( [10] => Doctor Zhivago [11] => War and Peace [12] => In Cold Blood [20] => Crime and Punishment [21] => Catch XII )
PHP has automatically created indexes 12 and 21.
<?php $countries = array( "de" => "Germany", "sk" => "Slovakia", "us" => "United States", "ru" => "Russia", "hu" => "Hungary", "pl" => "Poland"); print_r($countries); ?>
We create a
$countries
array. The indexes that we have mentioned earlier are actually keys in an associative array. The keys may be strings too.Perusing arrays
Next we will read the contents of the arrays. Again, there are several ways how we can display data from an array.
<?php $languages[10] = "PHP"; $languages[20] = "Python"; $languages[30] = "Ruby"; $languages[40] = "PERL"; $languages[50] = "Java"; echo $languages[10], "\n"; echo $languages[20], "\n"; echo $languages[30], "\n"; echo $languages[40], "\n"; echo $languages[50], "\n"; ?>
We can access data from an array by their index.
$ php peruse1.php PHP Python Ruby PERL Java
We have printed all five languages to the console.
<?php $continents = array("America", "Africa", "Europe", "Asia", "Antarctica"); $len = count($continents); for ($i = 0; $i < $len; $i++) { echo $continents[$i], "\n"; } ?>
In this example, we use the
for
statement to peruse a $continents
array.$len = count($continents);
First, we count the number of elements in the array.
for ($i = 0; $i < $len; $i++) { echo $continents[$i], "\n"; }
The
for
loop prints elements from the array by indexes 0..$len-1.<?php $continents = array("America", "Africa", "Europe", "Asia", "Antarctica"); foreach ($continents as $continent) { echo $continent, "\n"; } ?>
The easiest way to peruse an array is to use the
foreach
statement. The statement goes through the array one by one and puts a current element to the temporary $continent
variable. It accesses data without using their index or key.<?php $countries = array("de" => "Germany", "sk" => "Slovakia", "us" => "United States", "ru" => "Russia", "hu" => "Hungary", "pl" => "Poland"); function show_values($value, $key) { echo "The $key stands for the $value\n"; } array_walk($countries, show_values); ?>
In the last example, we use the
array_walk()
function to peruse an array. It applies a user function to every member of an array. The user function takes the key and the value of the item as parameters.$ php walk.php The de stands for the Germany The sk stands for the Slovakia The us stands for the United States The ru stands for the Russia The hu stands for the Hungary The pl stands for the Poland
We print both the key and the value to the console in the sentence.
In this part of the PHP tutorial, we worked with arrays. We initialized the arrays and read data from them. In the next chapter, we will work with various PHP array functions.
0 comments:
Post a Comment