PHP array functions
In the previous chapter, we dealt with array initialisation and perusal. In this chapter, we will cover various PHP array functions. These functions enable us to modify, sort, merge, slice, and shuffle the data inside the arrays.
First we are going to sort an array.
<?php $names = array("Jane", "Rebecca", "Lucy", "Lenka", "Ada"); foreach ($names as $name) { echo "$name "; } echo "\n"; sort($names); foreach ($names as $name) { echo "$name "; } echo "\n"; ?>
In the above script, we have a $names array. We use the
sort()
function to sort the contents of the array.$ php sort.php Jane Rebecca Lucy Lenka Ada Ada Jane Lenka Lucy Rebecca
The output of the script shows unsorted and sorted female names.
<?php $numbers = array(1, 2, 4, 5, 2, 3, 5, 2); $nums = count($numbers); $sum = array_sum($numbers); echo "In the array, there are $nums numbers\n"; echo "The sum of the numbers is $sum\n"; ?>
In the example, we have an array of numbers. We use the
count()
function to count the number of elements in the array. And the array_sum()
function to calculate the sum of all values.$ php sumcount.php In the array, there are 8 numbers The sum of the numbers is 24
This is the output of the script.
<?php $numbers = array(3, 4, 4, 3, 2, 4); $count_values = array_count_values($numbers); print_r($count_values); $unique = array_unique($numbers); print_r($unique); ?>
In this script, we have duplicates in the array. The
array_count_values()
function returns an array with the number of occurrences for each value. The array_unique()
function returns an array without duplicates.$ php unique.php Array ( [3] => 2 [4] => 3 [2] => 1 ) Array ( [0] => 3 [1] => 4 [4] => 2 )
The first array says that 3 is present twice, 4 three times and 2 once. The second array says that there are three values present in the array: 3, 4 and 2. Value 3 has index 0, 4 has index 1 and 2 has index 4. The
array_unique()
function keeps the indexes untouched.<?php $continents = array("America", "Africa", "Europe", "Asia", "Antarctica"); $item1 = current($continents); $item2 = next($continents); $item3 = next($continents); $item4 = end($continents); $item5 = prev($continents); echo "$item1, $item2, $item3, $item4, $item5\n"; reset($continents); while(list($idx, $val) = each($continents)) { echo "Index: $idx, Value: $val\n"; } ?>
In this example, we traverse the array using the functions that move the internal array pointer.
$item1 = current($continents); $item2 = next($continents); $item3 = next($continents); $item4 = end($continents); $item5 = prev($continents);
The
current()
function returns the current element in the array. At the beginning, it is the first element of the array. The next()
function advances the pointer by one position. The end()
function returns the last element. The prev()
element returns the element, one position before the current one. In our case it is the next to the last element.reset($continents); while(list($idx, $val) = each($continents)) { echo "Index: $idx, Value: $val\n"; }
Here we use the
reset()
function to set the internal pointer to the first element again. And peruse the $continents array one more time.<?php $names1 = array("Jane", "Lucy", "Rebecca"); $names2 = array("Lenka", "Timea", "Victoria"); $names = array_merge($names1, $names2); foreach ($names as $name) { echo "$name "; } echo "\n"; ?>
In this example, we have two arrays:
$names1
and $names2
. We use the array_merge()
function to create $names
array by merging the previous two arrays.$ php merge.php Jane Lucy Rebecca Lenka Timea Victoria
The new array has six names.
<?php $numbers = array(1, 2, 3, 4); array_push($numbers, 5, 6); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_pop($numbers); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_unshift($numbers, -1, 0); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; array_shift($numbers); foreach ($numbers as $num) { echo $num, " "; } echo "\n"; ?>
In the above script, we use functions that modify the contents of an array. We have a
$numbers
array that has 4 numbers: 1, 2, 3, and 4.array_push($numbers, 5, 6);
The
array_push()
function inserts one or more items to the end of the array. Our array now contains values 1, 2, 3, 4, 5, and 6.array_pop($numbers);
The
array_pop()
function removes the last item from the array. Our array stores now numbers 1, 2, 3, 4, and 5.array_unshift($numbers, -1, 0);
The
array_unshift()
function adds -1 and 0 to the beginning of the array. The array contains values -1, 0, 1, 2, 3, 4, and 5.array_shift($numbers);
Finally, the
array_shift()
function removes the first item from the array. Now we have values 0, 1, 2, 3, 4, and 5 in the array.<?php $numbers = range(1, 15); foreach ($numbers as $number) { echo "$number "; } echo "\n"; shuffle($numbers); foreach ($numbers as $number) { echo "$number "; } echo "\n"; ?>
In this script, we have two array functions. The
range()
function enables us to create a list of consecutive numbers easily. In our case, numbers 1, 2, ..15. We do not have to type them manually. And the shuffle()
function shuffles e.g. randomly relocates the elements of the array.$ php range.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9 15 13 7 3 10 11 4 12 5 6 14 1 2 8
Your output might look like this.
<?php $names = array("Jane", "Adriana", "Lucy", "Rebecca"); if (in_array("Jane", $names)) { echo "Jane is in the array\n"; } else { echo "Jane is not in the array\n"; } ?>
The
in_array()
function checks if a specific element is inside an array. Our script checks if 'Jane' is in the $names
array. The name is there, so we get the 'Jane is in the array' message to the console.<?php $domains = array("sk" => "Slovakia", "de" => "Germany", "hu" => "Hungary", "ru" => "Russia"); $keys = array_keys($domains); $values = array_values($domains); foreach ($keys as $key) { echo "$key "; } echo "\n"; foreach ($values as $value) { echo "$value "; } echo "\n"; ?>
The
array_keys()
function returns all the keys of an array. The array_values()
function returns all the values of an array.$ php keysvalues.php sk de hu ru Slovakia Germany Hungary Russia
The first line consists of top level domain names. These were the keys of the
$domains
array. The second line are the names of the corresponding countries. These were the values of the array.<?php $countries = array("de" => "Germany", "sk" => "Slovakia", "us" => "United States", "ru" => "Russia", "hu" => "Hungaria", "pl" => "Poland"); function show_values($value, $key) { echo "The $key stands for the $value\n"; } array_walk($countries, show_values); ?>
Finally, in our last example, we mention the
array_walk()
function. This function applies a user defined function to every member of the array. We have a $countries
array. We apply theshow_values
function to each element of the array. The function simply prints the key and the value for each element.
0 comments:
Post a Comment