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.
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.
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 thearray()
function to create an array. In its simplest form, the function takes an arbitrary number of comma separated values. <?phpHere we create a $names array. It stores 5 female names. The
$names = array("Jane", "Lucy", "Timea", "Beky", "Lenka");
print_r($names);
?>
print_r()
function prints a human readable information about the variable. $ php init1.phpFrom the output we can see the names and their indexes by which we can access them.
Array
(
[0] => Jane
[1] => Lucy
[2] => Timea
[3] => Beky
[4] => Lenka
)
<?phpThis 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.
$continents[0] = "America";
$continents[1] = "Africa";
$continents[2] = "Europe";
$continents[3] = "Asia";
$continents[4] = "Antarctica";
$continents[5] = "Australia";
print_r($continents);
?>
<?phpIn 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.
$continents = array(
1 => "America", 2 => "Africa",
3 => "America", 4 => "Asia",
5 => "Antarctica");
print_r($continents);
?>
$ php init22.phpNow we have an array of continents with indexes, that we have chosen.
Array
(
[1] => America
[2] => Africa
[3] => America
[4] => Asia
[5] => Antarctica
)
<?phpThe 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.
$languages[10] = "PHP";
$languages[20] = "Python";
$languages[30] = "Ruby";
$languages[40] = "PERL";
$languages[50] = "Java";
print_r($languages);
?>
<?phpWhen we do assignment initialization of a PHP array, we can omit the indexes. The PHP will automatically create the indexes for us.
$actors[] = "Philip Seymour Hoffman";
$actors[] = "Tom Cruise";
$actors[] = "Bill Paxton";
$actors[] = "Adrien Brody";
$actors[] = "Danie Craig";
print_r($actors);
?>
$ php init4.phpThe PHP interpreter has created consecutive indexes starting from zero.
Array
(
[0] => Philip Seymour Hoffman
[1] => Tom Cruise
[2] => Bill Paxton
[3] => Adrien Brody
[4] => Danie Craig
)
<?phpIn this script, we have omitted two indexes. The PHP will add them. It will create index 12 and index 21.
$novels[10] = "Doctor Zhivago";
$novels[11] = "War and Peace";
$novels[] = "In Cold Blood";
$novels[20] = "Crime and Punishment";
$novels[] = "Catch XII";
print_r($novels);
?>
$ php init5.phpPHP has automatically created indexes 12 and 21.
Array
(
[10] => Doctor Zhivago
[11] => War and Peace
[12] => In Cold Blood
[20] => Crime and Punishment
[21] => Catch XII
)
<?phpWe create a $countries array. The indexes that we have mentioned earlier are actually keys in an associative array. The keys may be strings too.
$countries = array(
"de" => "Germany", "sk" => "Slovakia",
"us" => "United States", "ru" => "Russia",
"hu" => "Hungary", "pl" => "Poland");
print_r($countries);
?>
Perusing arrays
Next we will read the contents of the arrays. Again, there are several ways how we can display data from an array.<?phpWe can access data from an array by their index.
$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";
?>
$ php peruse1.phpWe have printed all five languages to the console.
PHP
Python
Ruby
PERL
Java
<?phpIn this example, we use the
$continents = array("America", "Africa", "Europe", "Asia", "Antarctica");
$len = count($continents);
for ($i = 0; $i < $len; $i++) {
echo $continents[$i], "\n";
}
?>
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++) {The
echo $continents[$i], "\n";
}
for
loop prints elements from the array by indexes 0..$len-1. <?phpThe easiest way to peruse an array is to use the
$continents = array("America", "Africa", "Europe", "Asia", "Antarctica");
foreach ($continents as $continent) {
echo $continent, "\n";
}
?>
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. <?phpIn the last example, we use the
$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);
?>
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.phpWe print both the key and the value to the console in the sentence.
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
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