PHP sorting arrays
First we are going to sort an arrays.
sort.php
<?php $names = [ "Jane", "Rebecca", "Lucy", "Lenka", "Ada" ]; echo "Unsorted: \n"; foreach ($names as $name) { echo "$name "; } echo "\n"; sort($names); echo "Sorted: \n"; 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 Unsorted: Jane Rebecca Lucy Lenka Ada Sorted: Ada Jane Lenka Lucy Rebecca
The output of the script shows unsorted and sorted female names.
The rsort()
function sorts an array in reverse order.
sort2.php
<?php $numbers = [ 12, 3, 5, 1, 6, 7, 10, 0, 9, 8, 11]; sort($numbers); echo "Ascending order: \n"; foreach ($numbers as $n) { echo "$n "; } echo "\n"; rsort($numbers); echo "Descending order: \n"; foreach ($numbers as $n) { echo "$n "; } echo "\n"; ?>
There is an array of integers. It is sorted in ascending and descending order.
sort($numbers);
The sort()
function sorts the integers in ascending order.
rsort($numbers);
The rsort()
function sorts the integers in descending order.
$ php sort2.php Ascending order: 0 1 3 5 6 7 8 9 10 11 12 Descending order: 12 11 10 9 8 7 6 5 3 1 0
This is the output of the sort2.php
script.
In the following example, we show how to sort accented characters.
locale_sort.php
<?php setlocale(LC_ALL, 'sk_SK.utf8'); $words = [ "ďateľ", "auto", "železo", "byt", "kocka", "dáma", "zem", "autor", "ceduľa", "čižma"]; sort($words, SORT_LOCALE_STRING); echo "Ascending order: \n"; foreach ($words as $w) { echo "$w "; } echo "\n"; rsort($words, SORT_LOCALE_STRING); echo "Descending order: \n"; foreach ($words as $w) { echo "$w "; } echo "\n"; ?>
We have an array of Slovak words which contain specific accents.
setlocale(LC_ALL, 'sk_SK.utf8');
We set the Slovak locale using the setlocale()
function. A locale represents a specific geographical, political, or cultural region.
$words = [ "ďateľ", "auto", "železo", "byt", "kocka", "dáma", "zem", "autor", "ceduľa", "čižma"];
The $words
is an array of accented Slovak words.
sort($words, SORT_LOCALE_STRING);
We sort the array in ascending order with the sort()
function. We pass the SORT_LOCALE_STRING
flag to the function, which tells sort()
to take the locale into account.
$ php locale_sort.php Ascending order: auto autor byt ceduľa čižma dáma ďateľ kocka zem železo Descending order: železo zem kocka ďateľ dáma čižma ceduľa byt autor auto
The words are correctly sorted according to the Slovak standards.
Sometimes we need to perform custom sorting. For custom sorting, we have the usort()
function in PHP.
custom_sorting.php
<?php $names = [ "Michael Brown", "Albert Einstein", "Gerry Miller", "Tom Willis", "Michael Gray", "Luke Smith" ]; function sort_second_names($a, $b) { $name1 = explode(" ", $a); $name2 = explode(" ", $b); return strcmp($name1[1], $name2[1]); } usort($names, 'sort_second_names'); foreach ($names as $name) { echo "$name\n"; } echo "\n"; ?>
We have an array of full names. The sort()
function would sort these strings according to the first names, because they precede the second names. We create a solution to sort these names according to their second names.
function sort_second_names($a, $b) { $name1 = explode(" ", $a); $name2 = explode(" ", $b); return strcmp($name1[1], $name2[1]); }
We have a custom sorting function. The names are split by the explode()
function and the second names are compared with the strcmp()
function.
usort($names, 'sort_second_names');
The usort()
function accepts the comparing function as its second parameter.
$ php custom_sorting.php Michael Brown Albert Einstein Michael Gray Gerry Miller Luke Smith Tom Willis
The names are correctly sorted according to their second names.
PHP counting values in arrays
The count()
function counts the number of elements in the array. The array_sum()
function calculates the sum of all values. The array_product()
function calculates the product of values in the array.
counting.php
<?php $numbers = [ 1, 2, 4, 5, 2, 3, 5, 2 ]; $len = count($numbers); $sum = array_sum($numbers); $prod = array_product($numbers); echo "In the array, there are $len numbers\n"; echo "The sum of the numbers is $sum\n"; echo "The product of the numbers is $prod\n"; ?>
In the example, we have an array of numbers. We apply the above defined functions on the array.
$ php counting.php In the array, there are 8 numbers The sum of the numbers is 24 The product of the numbers is 2400
This is the output of the script.
PHP unique values
In the following example, we find out unique values in an array.unique.php
<?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 slicing arrays
The array_slice()
function returns a sequence of elements from an array as specified by its offset and length parameters.slicing.php
<?php $nums = range(1, 20); $slice1 = array_slice($nums, 0, 3); echo "Slice1:\n"; foreach ($slice1 as $s) { echo "$s "; } echo "\n"; $slice2 = array_slice($nums, -3); echo "Slice2:\n"; foreach ($slice2 as $s) { echo "$s "; } echo "\n"; ?>
In the example, we create two slices of an array of integers.
$slice1 = array_slice($nums, 0, 3);
We create a slice starting from the first element; the length of the slice is three elements.
$slice2 = array_slice($nums, -3);
By giving a negative offset, the slice is created from the end of the array.
$ php slicing.php Slice1: 1 2 3 Slice2: 18 19 20
This is the output of the slicing.php
example.
PHP array pointer
PHP has an internal array pointer. In the following example, we present functions that manipulate this pointer.
array_pointer.php
<?php $continents = [ "America", "Africa", "Europe", "Asia", "Australia", "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 array_pointer.php America, Africa, Europe, Antarctica, Australia Index: 0, Value: America Index: 1, Value: Africa Index: 2, Value: Europe Index: 3, Value: Asia Index: 4, Value: Australia Index: 5, Value: Antarctica
This is the output of the array_pointer.php
script.
PHP merging arrays
The array_merge()
function merges arrays.merge.php
<?php $names1 = [ "Jane", "Lucy", "Rebecca" ]; $names2 = [ "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 modifying arrays
It is possible to modify PHP arrays with array_push()
, array_pop()
, array_shift()
, or array_unshift()
functions.
modify.php
<?php $numbers = [ 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 modify.php 1 2 3 4 5 6 1 2 3 4 5 -1 0 1 2 3 4 5 0 1 2 3 4 5
This is the output of the modify.php
example.
PHP range function
The range()
function simplifies array creation by automatically creating a sequence of elements. It accepts three parameters: start of sequence, end of sequence, and an optional increment, which defaults to 1.range.php
<?php $numbers1 = range(1, 15); foreach ($numbers1 as $num) { echo "$num "; } echo "\n"; $numbers2 = range(15, 1, -1); foreach ($numbers2 as $num) { echo "$num "; } echo "\n"; ?>
The range()
function enables us to create a list of consecutive numbers easily.
$numbers1 = range(1, 15);
An array with numbers 1, 2, … 15 is created.
$numbers2 = range(15, 1, -1);
It is possible to create a descending sequence of values by specifying a negative increment.
$ php range.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
This is the output of the range.php
function.
PHP randomizing array values
The array_rand()
function picks one or more random entries from an array. The shuffle()
function randomizes the order of the elements in an array.
randomize.php
<?php $nums = range(1, 20); echo ($nums[array_rand($nums)]) . "\n"; $r = array_rand($nums, 2); echo $nums[$r[0]] . "\n"; echo $nums[$r[1]] . "\n"; shuffle($nums); foreach ($nums as $n) { echo "$n "; } echo "\n"; ?>
In the example, we pick random values from the array and randomize its order of elements.
echo ($nums[array_rand($nums)]) . "\n";
The array_rand()
function returns a random key from the $num
array.
$r = array_rand($nums, 2);
In this case, the array_rand()
function returns an array of two random keys.
$ php randomize.php 4 2 19 13 19 4 3 17 11 20 16 10 9 8 14 15 12 18 2 6 5 1 7
This is a sample output of the randomize.php
program.
PHP in_array function
The in_array()
function checks if a specific element is inside an array.
inarray.php
<?php $names = [ "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"; } if (in_array("Monica", $names)) { echo "Monica is in the array\n"; } else { echo "Monica is not in the array\n"; } ?>
Our script checks if ‘Jane’ and ‘Monica’ is in the $names
array.
$ php inarray.php Jane is in the array Monica is not in the array
‘Jane is in the array, but ‘Monica’ is not.
PHP array keys and values
PHP array is an associative array which consists of key and value pairs.
keysvalues.php
<?php $domains = [ "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 array_walk function
The array_walk()
function applies a user defined function to every member of the array.
array_walk.php
<?php $countries = [ "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'); ?>
We have a $countries
array. We apply the show_values
function to each element of the array. The function simply prints the key and the value for each element.
$ php array_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 Hungaria The pl stands for the Poland
This is the output of the array_walk()
function.