In the previous chapter, we dealt with array initialization and perusal. In this chapter, we will cover various PHP array functions. These functions enable us to modify, sort, merge, slice, shuffle the data inside the arrays.
First we are going to sort an array.
In this part of the PHP tutorial, we covered some PHP array functions.
First we are going to sort an array.
<?phpIn the above script, we have a $names array. We use the
$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";
?>
sort()
function to sort the contents of the array. $ php sort.phpThe output of the script shows unsorted and sorted female names.
Jane Rebecca Lucy Lenka Ada
Ada Jane Lenka Lucy Rebecca
<?phpIn the example, we have an array of numbers. We use the
$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";
?>
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.phpThis is the output of the script.
In the array, there are 8 numbers
The sum of the numbers is 24
<?phpIn this script, we have duplicates in the array. The
$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);
?>
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.phpThe 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. 3 has index 0, 4 has index 1 and 2 has index 4. The
Array
(
[3] => 2
[4] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 4
[4] => 2
)
array_unique()
function keeps the indexes untouched. <?phpIn this example, we traverse the array using the functions, that move the internal array pointer.
$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";
}
?>
$item1 = current($continents);The
$item2 = next($continents);
$item3 = next($continents);
$item4 = end($continents);
$item5 = prev($continents);
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);Here we use the
while(list($idx, $val) = each($continents)) {
echo "Index: $idx, Value: $val\n";
}
reset()
function to set the internal pointer to the first element again. And peruse the $continents array one more time. <?phpIn this example, we have two arrays. $names1 and $names2. We use the
$names1 = array("Jane", "Lucy", "Rebecca");
$names2 = array("Lenka", "Timea", "Victoria");
$names = array_merge($names1, $names2);
foreach ($names as $name) {
echo "$name ";
}
echo "\n";
?>
array_merge()
function to create $names array by merging the previous two arrays. $ php merge.phpThe new array has six names.
Jane Lucy Rebecca Lenka Timea Victoria
<?phpIn the above script, we use functions that modify the contents of an array. We have a $nubmers array, that has 4 numbers. 1, 2, 3, 4.
$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";
?>
array_push($numbers, 5, 6);The
array_push()
function inserts one or more items to the end of the array. Our array now has 1, 2, 3, 4, 5, 6. array_pop($numbers);The
array_pop()
function removes the last item from the array. Our array stores now 1, 2, 3, 4, 5. array_unshift($numbers, -1, 0);The
array_unshift()
function adds -1, 0 to the beginning of the array. The array has -1, 0, 1, 2, 3, 4, 5. array_shift($numbers);Finally, the
array_shift()
function removes the first item from the array. Now we have 0, 1, 2, 3, 4, 5 in the array. <?phpIn this script, we have two array functions. The
$numbers = range(1, 15);
foreach ($numbers as $number) {
echo "$number ";
}
echo "\n";
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
}
echo "\n";
?>
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.phpYour output might look like this.
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
<?phpThe
$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";
}
?>
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. <?phpThe
$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";
?>
array_keys()
function returns all the keys of an array. The array_values()
function returns all the values of an array. $ php keysvalues.phpThe 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.
sk de hu ru
Slovakia Germany Hungary Russia
<?phpFinally, in our last example, we mention the
$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);
?>
array_walk()
function. This function applies a user defined function to every member of the array. 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. In this part of the PHP tutorial, we covered some PHP array functions.
0 comments:
Post a Comment