Last active
December 9, 2021 21:20
-
-
Save roberto-butti/e8a1f5a4bae04b3a9668896ac546a24c to your computer and use it in GitHub Desktop.
arr-cheatsheet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Cheatsheet with PHP array functions | |
* | |
* Cheatsheet for showing some "use cases" about | |
* PHP array functions, sorting, editing, map, filter etc. | |
* | |
* @author Roberto Butti | |
* @link https://gist.github.com/roberto-butti/e8a1f5a4bae04b3a9668896ac546a24c | |
* | |
*/ | |
/** | |
* Print a line for string, integer, array, boolean. | |
*/ | |
function print_result(mixed $something): void | |
{ | |
switch (gettype($something)) { | |
case 'string': | |
echo "STRING : " . $something . PHP_EOL; | |
break; | |
case 'integer': | |
echo "INTEGER : " . $something . PHP_EOL; | |
break; | |
case 'array': | |
echo "ARRAY : " . implode(",", $something) . PHP_EOL; | |
break; | |
case 'boolean': | |
echo "BOOLEAN : " . (($something) ? "true" : "false") . PHP_EOL; | |
break; | |
default: | |
var_dump($something); | |
break; | |
} | |
} | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// Returns true if all elements return true | |
$arr = [1,2,3,4,5]; | |
$result = array_reduce($arr, fn($result, $element) => $result && ($element > 0), true); | |
print_result($result); | |
// BOOLEAN : true | |
// Returns true if at least one element return true | |
$arr = [1,2,3,4,5]; | |
$result = array_reduce($arr, fn($result, $element) => $result || ($element >= 5), false); | |
print_result($result); | |
// BOOLEAN : true | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// 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 | |
// Removes and returns last element from arr | |
$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 | |
// Add element to start of arr 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 | |
// 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 | |
// Reverse order of arr | |
$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 | |
// Sort the elements of arr | |
$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 | |
// 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 | |
// Returns a string representing arr its elements (same as arr.join(',')) | |
$arr = [1,2,3,4,5,6,7,8,9]; | |
$result = implode("", $arr); | |
print_result($result); | |
// STRING : 123456789 | |
// Returns length of arr | |
$arr = [1,2,3,4,5,6,7,8,9]; | |
$result = count($arr); | |
print_result($result); | |
// INTEGER : 9 | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment