Navigating arrays is an art, and PHP’s functions are your paintbrushes
Getting close to completion. In the last article we looked at string functions and in this one we’re going to take a look at all the various and common array functions.
https://www.dinocajic.com/string-functions/
trim(),
ltrim()
,rtrim()
htmlspecialchars()
__call()
preg_match()
filter_var()
addslashes()
str_replace()
strlen()
strtolower()
strtoupper()
ucfirst()
strpos()
,stripos()
,strrpos()
,strripos()
- Array Functions like:
array_chunk()
,array_diff()
,array_key_exists()
,array_key_first()
,array_key_last()
,array_map()
,array_merge()
,array_push()
,array_sum()
,asort()
,arsort()
,count()
,in_array()
,ksort()
,krsort()
,sort()
,rsort()
,shuffle()
,sizeof()
,is_array()
,explode()
,implode()
- Magic Methods like:
__invoke()
,__toString()
array_chunk()
If you have an array, and you want to make multiple smaller arrays out of it, you can use the array_chunk
method. The first parameter is the large array, the second parameter is the amount of elements in each of the chunks and the third parameter dictates whether you want to preserve the keys or not.
<?php
$array = ["Hello", "There", "How", "Are", "You", "Doing", "Today"];
var_dump( array_chunk($array, 2) );
/app/98 Functions/ArrayFunctions.php:4:
array (size=4)
0 =>
array (size=2)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
1 =>
array (size=2)
0 => string 'How' (length=3)
1 => string 'Are' (length=3)
2 =>
array (size=2)
0 => string 'You' (length=3)
1 => string 'Doing' (length=5)
3 =>
array (size=1)
0 => string 'Today' (length=5)
<?php
$array = ["Hello", "There", "How", "Are", "You", "Doing", "Today"];
var_dump( array_chunk($array, 3) );
/app/98 Functions/ArrayFunctions.php:5:
array (size=3)
0 =>
array (size=3)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
2 => string 'How' (length=3)
1 =>
array (size=3)
0 => string 'Are' (length=3)
1 => string 'You' (length=3)
2 => string 'Doing' (length=5)
2 =>
array (size=1)
0 => string 'Today' (length=5)
<?php
$array = ["Hello", "There", "How", "Are", "You", "Doing", "Today"];
var_dump( array_chunk($array, 3, true) );
/app/98 Functions/ArrayFunctions.php:6:
array (size=3)
0 =>
array (size=3)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
2 => string 'How' (length=3)
1 =>
array (size=3)
3 => string 'Are' (length=3)
4 => string 'You' (length=3)
5 => string 'Doing' (length=5)
2 =>
array (size=1)
6 => string 'Today' (length=5)
<?php
$array = [
"first_key" => "Hello",
"second_key" => "There",
"third_key" => "How",
"fourth_key" => "Are",
"fifth_key" => "You",
"sixth_key" => "Doing",
"seventh_key" => "Today"
];
var_dump( array_chunk($array, 2) );
/app/98 Functions/ArrayFunctions.php:17:
array (size=4)
0 =>
array (size=2)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
1 =>
array (size=2)
0 => string 'How' (length=3)
1 => string 'Are' (length=3)
2 =>
array (size=2)
0 => string 'You' (length=3)
1 => string 'Doing' (length=5)
3 =>
array (size=1)
0 => string 'Today' (length=5)
<?php
$array = [
"first_key" => "Hello",
"second_key" => "There",
"third_key" => "How",
"fourth_key" => "Are",
"fifth_key" => "You",
"sixth_key" => "Doing",
"seventh_key" => "Today"
];
var_dump( array_chunk($array, 3) );
/app/98 Functions/ArrayFunctions.php:18:
array (size=3)
0 =>
array (size=3)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
2 => string 'How' (length=3)
1 =>
array (size=3)
0 => string 'Are' (length=3)
1 => string 'You' (length=3)
2 => string 'Doing' (length=5)
2 =>
array (size=1)
0 => string 'Today' (length=5)
<?php
$array = [
"first_key" => "Hello",
"second_key" => "There",
"third_key" => "How",
"fourth_key" => "Are",
"fifth_key" => "You",
"sixth_key" => "Doing",
"seventh_key" => "Today"
];
var_dump( array_chunk($array, 3, true) );
/app/98 Functions/ArrayFunctions.php:19:
array (size=3)
0 =>
array (size=3)
'first_key' => string 'Hello' (length=5)
'second_key' => string 'There' (length=5)
'third_key' => string 'How' (length=3)
1 =>
array (size=3)
'fourth_key' => string 'Are' (length=3)
'fifth_key' => string 'You' (length=3)
'sixth_key' => string 'Doing' (length=5)
2 =>
array (size=1)
'seventh_key' => string 'Today' (length=5)
array_diff()
When you have two arrays, and want to know which values are not present in both arrays, you can use the array_diff
function. It only looks at the values, so even if the keys are different, as long as the values are the same, it treats the elements as appearing in both arrays.
<?php
$first = ["first_key" => "value", "Hey", "How", "Are", "You"];
$second = ["second_key" => "value", "Hey", "How", "You"];
var_dump( array_diff($first, $second) );
/app/98 Functions/ArrayFunctions.php:26:
array (size=2)
'first_key' => string 'value' (length=5)
2 => string 'Are' (length=3)
array_key_exists()
If we need to check whether a specific array key exists inside of the array, we can check it with the array_key_exists
function.
<?php
$array = [
"first_key" => "Hello",
"second_key" => "There",
"third_key" => "How",
];
var_dump( array_key_exists("fourth_key", $array) ); // false
var_dump( array_key_exists("second_key", $array) ); // true
array_key_first(), array_key_last()
The array_key_first
function get the first array key. And the array_key_last
function gets the last array key in an array.
<?php
$array = [
"first_key" => "Hello",
"second_key" => "There",
"third_key" => "How",
];
var_dump( array_key_first($array) );
var_dump( array_key_last($array) );
/app/98 Functions/ArrayFunctions.php:45:string 'first_key' (length=9)
/app/98 Functions/ArrayFunctions.php:46:string 'third_key' (length=9)
array_merge()
The array_merge
function merges one or more arrays. We’ll look at merging two and three arrays together.
<?php
$first = ["first_key" => "value", "Hey", "How", "Are", "You"];
$second = ["second_key" => "value", "Hey", "How", "You"];
$third = ["third_key" => "value", "Hey", "How", "You"];
var_dump( array_merge($first, $second) );
var_dump( array_merge($first, $second, $third) );
/app/98 Functions/ArrayFunctions.php:53:
array (size=9)
'first_key' => string 'value' (length=5)
0 => string 'Hey' (length=3)
1 => string 'How' (length=3)
2 => string 'Are' (length=3)
3 => string 'You' (length=3)
'second_key' => string 'value' (length=5)
4 => string 'Hey' (length=3)
5 => string 'How' (length=3)
6 => string 'You' (length=3)
/app/98 Functions/ArrayFunctions.php:54:
array (size=13)
'first_key' => string 'value' (length=5)
0 => string 'Hey' (length=3)
1 => string 'How' (length=3)
2 => string 'Are' (length=3)
3 => string 'You' (length=3)
'second_key' => string 'value' (length=5)
4 => string 'Hey' (length=3)
5 => string 'How' (length=3)
6 => string 'You' (length=3)
'third_key' => string 'value' (length=5)
7 => string 'Hey' (length=3)
8 => string 'How' (length=3)
9 => string 'You' (length=3)
What we can see is that values are not eliminated. It doesn’t care that the exact same values are present in multiple arrays. It just merges everything together.
What would happen if we had two array keys that were named the same?
<?php
$first = ["first_key" => "value one"];
$second = ["first_key" => "value two"];
var_dump( array_merge($first, $second) );
In this instance, the last value is taken and assigned to the array key. So be careful with array_merge
if you’re using named indices.
/app/98 Functions/ArrayFunctions.php:59:
array (size=1)
'first_key' => string 'value two' (length=9)
array_push()
The array_push
functions adds an element to the end of an array.
<?php
$array = ["Hello", "There"];
array_push($array, "How", "Are", "You");
var_dump($array);
One common mistake is to assign the array_push
back to the array. The first argument, the array, is passed as reference, meaning that any modifications that are done to it automatically reflect back in the array.
/app/98 Functions/ArrayFunctions.php:65:
array (size=5)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
2 => string 'How' (length=3)
3 => string 'Are' (length=3)
4 => string 'You' (length=3)
You can also do this with the open/close brackets without an index value.
<?php
$array = ["Hello", "There"];
$array[] = "How";
$array[] = "Are";
$array[] = "You";
var_dump($array);
/app/98 Functions/ArrayFunctions.php:72:
array (size=5)
0 => string 'Hello' (length=5)
1 => string 'There' (length=5)
2 => string 'How' (length=3)
3 => string 'Are' (length=3)
4 => string 'You' (length=3)
array_sum()
The array_sum
function calculates the value of the sum of all the integer/float values in the array.
<?php
$array = [1, 2.2, 3, 4, "named_key" => 6];
var_dump( array_sum($array) );
/app/98 Functions/ArrayFunctions.php:76:float 16.2
asort()
Sorts the array by value. It does not care about whether the array has named keys or numeric keys, it just looks at the value. The asort function keeps the index association.
<?php
$first = ["E", "C", "B", "A"];
$second = ["E", "c", "B", "A"];
$third = ["E", 1, "B", 63];
$fourth = ["E", "first_key" => 1, "second_key" => "B", 63];
asort($first);
asort($second);
asort($third);
asort($fourth);
var_dump( $first );
var_dump( $second );
var_dump( $third );
var_dump( $fourth );
/app/98 Functions/ArrayFunctions.php:89:
array (size=4)
3 => string 'A' (length=1)
2 => string 'B' (length=1)
1 => string 'C' (length=1)
0 => string 'E' (length=1)
/app/98 Functions/ArrayFunctions.php:90:
array (size=4)
3 => string 'A' (length=1)
2 => string 'B' (length=1)
0 => string 'E' (length=1)
1 => string 'c' (length=1)
/app/98 Functions/ArrayFunctions.php:91:
array (size=4)
1 => int 1
3 => int 63
2 => string 'B' (length=1)
0 => string 'E' (length=1)
/app/98 Functions/ArrayFunctions.php:92:
array (size=4)
'first_key' => int 1
1 => int 63
'second_key' => string 'B' (length=1)
0 => string 'E' (length=1)
arsort()
To sort the array by value in reverse, and maintain the index association, use arsort
.
<?php
$first = ["E", "C", "B", "A"];
$second = ["E", "c", "B", "A"];
$third = ["E", 1, "B", 63];
$fourth = ["E", "first_key" => 1, "second_key" => "B", 63];
arsort($first);
arsort($second);
arsort($third);
arsort($fourth);
var_dump( $first );
var_dump( $second );
var_dump( $third );
var_dump( $fourth );
/app/98 Functions/ArrayFunctions.php:105:
array (size=4)
0 => string 'E' (length=1)
1 => string 'C' (length=1)
2 => string 'B' (length=1)
3 => string 'A' (length=1)
/app/98 Functions/ArrayFunctions.php:106:
array (size=4)
1 => string 'c' (length=1)
0 => string 'E' (length=1)
2 => string 'B' (length=1)
3 => string 'A' (length=1)
/app/98 Functions/ArrayFunctions.php:107:
array (size=4)
0 => string 'E' (length=1)
2 => string 'B' (length=1)
3 => int 63
1 => int 1
/app/98 Functions/ArrayFunctions.php:108:
array (size=4)
0 => string 'E' (length=1)
'second_key' => string 'B' (length=1)
1 => int 63
'first_key' => int 1
count()
The count
function returns the number of array elements inside of the array. A common error is thinking that if one array element is inside of the array, the answer should be 0
. Remember that this doesn’t have anything to do with index values. It’s the count of elements.
<?php
$array = ["E", "C", "B", "A"];
var_dump( count($array) );
/app/98 Functions/ArrayFunctions.php:113:int 4
sizeof()
Alias of the count
function.
<?php
$array = ["E", "C", "B", "A"];
var_dump( sizeof($array) );
/app/98 Functions/ArrayFunctions.php:176:int 4
in_array()
To check whether a value (not key) exists inside of an array, we would use the in_array
function.
<?php
$first = ["E", 1, "B", 63];
$second = ["E", "first_key" => 1, "second_key" => "B", 63];
var_dump( in_array("E", $first) ); // true
var_dump( in_array("A", $second) ); // false
var_dump( in_array(63, $second) ); // true
var_dump( in_array("first_key", $second) ); // false
ksort()
To sort the array by the key in ascending order, use ksort
.
<?php
$first = ["E", 1, "B", 63];
$second = ["E", "z_key" => 1, "a_key" => "B", 63];
ksort($first);
ksort($second);
/app/98 Functions/ArrayFunctions.php:131:
array (size=4)
0 => string 'E' (length=1)
1 => int 1
2 => string 'B' (length=1)
3 => int 63
/app/98 Functions/ArrayFunctions.php:132:
array (size=4)
0 => string 'E' (length=1)
'a_key' => string 'B' (length=1)
'z_key' => int 1
1 => int 63
It’s really interesting that a_key
and z_key
are sorted between 0
and 1
.
krsort()
To sort an array by key in descending order, use krsort
.
<?php
// krsort
$first = ["E", 1, "B", 63];
$second = ["E", "z_key" => 1, "a_key" => "B", 63];
krsort($first);
krsort($second);
var_dump( $first );
var_dump( $second );
/app/98 Functions/ArrayFunctions.php:141:
array (size=4)
3 => int 63
2 => string 'B' (length=1)
1 => int 1
0 => string 'E' (length=1)
/app/98 Functions/ArrayFunctions.php:142:
array (size=4)
1 => int 63
0 => string 'E' (length=1)
'z_key' => int 1
'a_key' => string 'B' (length=1)
What’s even more interesting is when sorting in reverse. You would think that a_key
and z_key
would still be between 0
and 1
, but as you can see, that’s not the case. Be careful when sorting mixed keys (index and named).
sort()
The sort
function sorts the array by value in ascending order and removes all array key associations.
<?php
// sort
$first = ["E", 1, "B", 63];
$second = ["E", "z_key" => 1, "a_key" => "B", 63];
sort($first);
sort($second);
var_dump( $first );
var_dump( $second );
/app/98 Functions/ArrayFunctions.php:151:
array (size=4)
0 => int 1
1 => int 63
2 => string 'B' (length=1)
3 => string 'E' (length=1)
/app/98 Functions/ArrayFunctions.php:152:
array (size=4)
0 => int 1
1 => int 63
2 => string 'B' (length=1)
3 => string 'E' (length=1)
rsort()
The rsort
function sorts the array by value in descending order and removes all array key associations.
<?php
$first = ["E", 1, "B", 63];
$second = ["E", "z_key" => 1, "a_key" => "B", 63];
rsort($first);
rsort($second);
var_dump( $first );
var_dump( $second );
/app/98 Functions/ArrayFunctions.php:161:
array (size=4)
0 => string 'E' (length=1)
1 => string 'B' (length=1)
2 => int 63
3 => int 1
/app/98 Functions/ArrayFunctions.php:162:
array (size=4)
0 => string 'E' (length=1)
1 => string 'B' (length=1)
2 => int 63
3 => int 1
shuffle()
The shuffle
function shuffles the array.
<?php
$array = ["first_key" => "value", "Hey", "How", "Are", "You"];
shuffle($array);
var_dump($array);
shuffle($array);
var_dump($array);
/app/98 Functions/ArrayFunctions.php:168:
array (size=5)
0 => string 'How' (length=3)
1 => string 'You' (length=3)
2 => string 'Are' (length=3)
3 => string 'value' (length=5)
4 => string 'Hey' (length=3)
/app/98 Functions/ArrayFunctions.php:171:
array (size=5)
0 => string 'You' (length=3)
1 => string 'Are' (length=3)
2 => string 'value' (length=5)
3 => string 'How' (length=3)
4 => string 'Hey' (length=3)
is_array()
To check whether the variable contains an array, we can use the is_array
function.
<?php
$first = ["A", "B"];
$second = "Dino";
var_dump( is_array($first) ); // true
var_dump( is_array($second) ); // false
explode()
The explode
function converts a string into an array at a specific delimiter.
<?php
$string = "Hey there. How are you?";
$first = explode(" ", $string);
$second = explode(".", $string);
var_dump( $first );
var_dump( $second );
/app/98 Functions/ArrayFunctions.php:190:
array (size=5)
0 => string 'Hey' (length=3)
1 => string 'there.' (length=6)
2 => string 'How' (length=3)
3 => string 'are' (length=3)
4 => string 'you?' (length=4)
/app/98 Functions/ArrayFunctions.php:191:
array (size=2)
0 => string 'Hey there' (length=9)
1 => string ' How are you?' (length=13)
implode()
The implode
function takes an array and converts it into a string. You can add the separator of your choice.
<?php
$array = ["Hey,", "How", "Are", "You?"];
$string_one = implode(" ", $array);
$string_two = implode("-", $array);
var_dump($string_one);
var_dump($string_two);
/app/98 Functions/ArrayFunctions.php:198:string 'Hey, How Are You?' (length=17)
/app/98 Functions/ArrayFunctions.php:199:string 'Hey,-How-Are-You?' (length=17)
array_map()
The last array function that we’ll cover is the array_map
function. The array_map
function requires a callback function for its first argument, and the array for its second argument. The callback function runs for each of the array elements. So, imagine looping through an array and applying some operation on each value. Once the operation is done, you would reasign it back to the array. If the array was [1, 2, 3]
and we wanted to double each array element, we would multiply each value by 2
and assign it back to the array to get [2, 4, 6]
.
function multiply_by_two( $n )
{
return ($n * 2);
}
$a = [1, 2, 3];
$b = array_map('multiply_by_two', $a);
var_dump( $a );
var_dump( $b );
/app/98 Functions/ArrayFunctions.php:210:
array (size=3)
0 => int 1
1 => int 2
2 => int 3
/app/98 Functions/ArrayFunctions.php:211:
array (size=3)
0 => int 2
1 => int 4
2 => int 6
To make this more of a real-life example, we can pass the anonymous function as the first argument.
<?php
$a = [1, 2, 3];
$b = array_map(function($n) {
return ($n * 2);
}, $a);
var_dump( $a );
var_dump( $b );
We can condense it even further, buy moving the array itself as the second argument instead of assigning it first to $a
.
<?php
$b = array_map(function($n) {
return ($n * 2);
}, [1, 2, 3]);
var_dump( $b );
We can finally make the anonymous function an arrow function.
<?php
$b = array_map( fn($n) => $n * 2, [1, 2, 3] );
var_dump( $b );
If you need a recap of callbacks, arrow functions, or anonymous functions, I got you covered.
https://www.dinocajic.com/php-use-keyword/
https://www.dinocajic.com/php-arrow-functions/
https://www.dinocajic.com/php-callback-functions/
And we’re done with array functions. A few more built in functions to go.
https://github.com/dinocajic/php-youtube-tutorials?source=post_page—–54f682a76c1——————————
STRINGS BEND TO YOUR WILL WITH PHP’S DYNAMIC FUNCTIONS
These are simple built-in functions that require little to grasp. They’re conveniently named to simplify what the operations will accomplish.
Navigating arrays is an art, and PHP’s functions are your paintbrushes
PHP – P105: array functions
Array functions in PHP are like a versatile toolkit, offering an array of tools to slice, dice, rearrange, and transform data arrays effortlessly, allowing developers to compose intricate data symphonies with precision and elegance.
CLASS WIZARDRY IN A NUTSHELL.
PHP – P106: BUILT IN FUNCTIONS: _INVOKE, _TOSTRING, _GET, AND _SET
PHP’s magic methods, including __invoke for object invocation, __toString for string conversion, and __get and __set for dynamic property handling, elevate class flexibility and clarity.