DEV Community

Cover image for Cheat sheet for PHP array functions
Roberto B.
Roberto B.

Posted on

Cheat sheet for PHP array functions

Cheat sheet for showing some "use cases" about PHP array functions, sorting, editing, map, filter etc.

Append arrays

Returns a new array, appending input arrays with array unpacking

$arr = [1,2,3,4,5];
$output = [...$arr, ...[10,11,12]];
print_result($output);
// ARRAY   : 1,2,3,4,5,10,11,12
Enter fullscreen mode Exit fullscreen mode

Returns a new array, appending input arrays with array_merge

$arr = [1,2,3,4,5];
$output = array_merge($arr, [10,11,12]);
print_result($output);
// ARRAY   : 1,2,3,4,5,10,11,12
Enter fullscreen mode Exit fullscreen mode

Joins elements into string

Joins all elements into a string with a separator

$arr = [1,2,3,4,5];
$output = implode(",", $arr);
print_result($output);
// STRING  : 1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

A section of an array

Returns a section of an array from $start to $end

$start = 0;
$end = 3;
$arr = [1,2,3,4,5];
$output = array_slice($arr, $start, $end);
print_result($output);
// ARRAY   : 1,2,3
Enter fullscreen mode Exit fullscreen mode

Find the first occurrence

Returns the index of the first occurrence of element in array

$arr = [1,2,3,4,5, 1,2,3,4,5];
$element = 4;
$output = array_search($element, $arr);
print_result($output);
// INTEGER : 3
Enter fullscreen mode Exit fullscreen mode

Find the last occurrence

Returns the index of the last occurrence of element in array

$arr = [1,2,3,4,5, 1,2,3,4,5];
$element = 4;
$output = array_search(4, array_reverse($arr, true));
print_result($output);
// INTEGER : 8
Enter fullscreen mode Exit fullscreen mode

A function for each element

Calls function for each element of array (the array is unchanged)

$arr = [1,2,3,4,5];
$output = array_walk($arr, function ($element) {
    $double = $element * 2;
});
print_result($output);
print_result($arr);
// BOOLEAN : true
// ARRAY   : 1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

All elements match an expression

Returns true if all elements match a condition

$arr = [1,2,3,4,5];
$result = array_reduce($arr, fn($result, $element) => $result && ($element > 0), true);
print_result($result);
// BOOLEAN : true
Enter fullscreen mode Exit fullscreen mode

At least 1 element matches an expression

Returns true if at least 1 element matches a condition

$arr = [1,2,3,4,5];
$result = array_reduce($arr, fn($result, $element) => $result || ($element >= 5), false);
print_result($result);
// BOOLEAN : true
Enter fullscreen mode Exit fullscreen mode

Filter elements

Returns new array, with element that matches the fn() function

$arr = [1,2,3,4,5];
$result = array_filter($arr, fn($element) => $element > 3);
print_result($result);
// ARRAY   : 4,5
Enter fullscreen mode Exit fullscreen mode

Apply a function for each element

Returns new array with the results of running fn on every element

$arr = [1,2,3,4,5];
$result = array_map(fn($element) => $element + 3, $arr);
print_result($result);
// ARRAY   : 4,5,6,7,8
Enter fullscreen mode Exit fullscreen mode

Flatten array

Returns a flatten array of a nested array

$nestedArray = [1, [2,3], [4,5,6], 7];
$result = array_reduce($nestedArray, fn($result, $element) => array_merge($result, is_array($element) ? [...$element] : [$element]), []);
print_result($result);
// ARRAY   : 1,2,3,4,5,6,7
Enter fullscreen mode Exit fullscreen mode

Change some elements

Changes all elements in range to a the specified value

$arr = [1,2,3,4,5,6,7,8,9];
$start = 2;
$count = 6;
$result = array_replace($arr, array_fill($start, $count, 88));
print_result($result);
// ARRAY   : 1,2,88,88,88,88,88,88,9
Enter fullscreen mode Exit fullscreen mode

Apply a function with all elements

Returns a single value which is the function's accumulated result L2R

$arr = [1,2,3,4,5,6,7,8,9];
$result = array_reduce($arr, fn($result, $element) => $result + $element, 0);
print_result($result);
// INTEGER : 45
Enter fullscreen mode Exit fullscreen mode

Returns a single value which is the function's accumulated result R2L

$arr = [1,2,3,4,5,6,7,8,9];
$result = array_reduce(array_reverse($arr), fn($result, $element) => $result + $element, 0);
print_result($result);
// INTEGER : 45
Enter fullscreen mode Exit fullscreen mode

Last element

Removes and returns last element from array

$arr = [1,2,3,4,5,6,7,8,9];
$result = array_pop($arr);
print_result($result);
print_result($arr);
// INTEGER : 9
// ARRAY   : 1,2,3,4,5,6,7,8
Enter fullscreen mode Exit fullscreen mode

Prepends an element

Add element at the start of an array and return new length

$arr = [1,2,3,4,5,6,7,8,9];
$newElement = 99;
$result = array_unshift($arr, $newElement);
print_result($result);
print_result($arr);
// INTEGER : 10
// ARRAY   : 99,1,2,3,4,5,6,7,8,9
Enter fullscreen mode Exit fullscreen mode

Add element at the end

Adds element to the end of arr and returns new length

$arr = [1,2,3,4,5,6,7,8,9];
$newElement = 99;
$result = array_push($arr, 99);
print_result($result);
print_result($arr);
// INTEGER : 10
// ARRAY   : 1,2,3,4,5,6,7,8,9,99
Enter fullscreen mode Exit fullscreen mode

Reverse an array

Reverse order of an array

$arr = [1,2,3,4,5,6,7,8,9];
$result = array_reverse($arr);
print_result($result);
// ARRAY   : 9,8,7,6,5,4,3,2,1
Enter fullscreen mode Exit fullscreen mode

Sorting

Sort the elements of an array

$arr = [5,3,4,1,2,9,7,8,6];
$result = sort($arr);
print_result($arr);
// ARRAY   : 1,2,3,4,5,6,7,8,9
Enter fullscreen mode Exit fullscreen mode

Remove/replacing/adding elements

Changes content of arr removing, replacing and adding elements at position $start, it removes $count elements and insert it returns the elements removed, and changes the $arr

$arr = [1,2,3,4,5,6,7,8,9];
$start = 4;
$count = 3;
$newElements = [70,71];
$result = array_splice($arr, $start, $count, $newElements);
print_result($result); // removed elements
print_result($arr); // changed array
// ARRAY   : 5,6,7
// ARRAY   : 1,2,3,4,70,71,8,9
Enter fullscreen mode Exit fullscreen mode

Stringify and array

Returns a string representing arr its elements

$arr = [1,2,3,4,5,6,7,8,9];
$result = implode("", $arr);
print_result($result);
// STRING  : 123456789
Enter fullscreen mode Exit fullscreen mode

Length

Returns length of an array

$arr = [1,2,3,4,5,6,7,8,9];
$result = count($arr);
print_result($result);
// INTEGER : 9
Enter fullscreen mode Exit fullscreen mode

Is array?

Returns true if $arr is an array

$arr = [1,2,3,4,5,6,7,8,9];
$result = is_array($arr);
print_result($result);
// BOOLEAN : true
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
robertobutti profile image
Roberto B.

Starting from this post, I. created a little PHP package for managing arrays exposing methods similar to Array object in Javascript.
github.com/Hi-Folks/array