Some commonly used:
Click here to see all of them
Examples:
array_push($arr, variable) - push (add) one or more elements into the end of array:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_push($arr, 11, 12, 13, 100);
echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 100
array_pop($arr) - Pop (remove) the element of the end of the array:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_pop($arr);
echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8, 9
array_unshift($arr, variable) - Prepend one or more elements to the beginning of an array:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_unshift($arr, -1, 0);
echo implode(", ", $arr);
// $arr = -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
array_shift($arr) – Shift (remove) an element off the beginning of array:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_shift($arr);
echo implode(", ", $arr);
// $arr = 2, 3, 4, 5, 6, 7, 8, 9, 10
array_splice($arr, offset, length, replacement): Removes a portion of the array and replace it with something else and return removed elements:
Remove functionality:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_splice($arr, 8, 2); //Removes 9 and 10
echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8
Replace functionality:
$arr = [1, 2, 3, 4, 5, 6, 7, 8];
array_splice($arr, 3, 2, 3.3);
echo implode(", ", $arr);
// $arr = 1, 2, 3, 3.3, 6, 7, 8
array_unique($arr) - Removes duplicate values from an array:
$names = ["Peter", "George", "Toni", "George", "Peter", "Gabriel"];
$names = array_unique($names);
echo implode(", ", $names);
//$names = Peter, George, Toni, Gabriel
array_reverse($arr) - Returns an array in reverse order:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$arr = array_reverse($arr);
echo implode(", ", $arr);
//$arr = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
array_sum($arr) - Calculate the sum of values in an array:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$sum = array_sum($arr);
echo $sum;
//$sum =55
array_filter($arr) - Removes empty array elements or values + also removes null, false, 0 (zero) values:
$arr = explode(" ", "Hi, my name is Peter");
var_dump($arr);
// A lot of empty spaces in $arr (13 elements)
$arr = array_filter($arr);
var_dump($arr);
//$arr (5 elements)
array_search(searchValue, array) - Searches for a given value and returns the first corresponding key if successful. Searches for needle in haystack:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$el = array_search(9, $arr);
echo $el;
//$el = 8
in_array(searchValue, $arr, true) - Checks if a value exists in an array (3rd parameter is optional). Check the types of searchValue. Like array_search but returns boolean: true or false:
$arr = [1, 2, 3, '4', 5, 6, 7, 8, 9, 10];
var_dump($isContain = in_array(10, $arr));
// true or 1
var_dump($isContain = in_array(22, $arr));
// false or 0 or empty string
var_dump($isContain = in_array('4', $arr, true));
// true or 1
var_dump($isContain = in_array(4, $arr, true));
// false or 0 or empty string
Sorting Array:
sort($arr) - Sort in natural (ascending) order:
$names = ["George", "Allan", "Peter"];
sort($names);
var_dump($names);
//$names = ["Allan", "George", "Peter"]
rsort($arr) - Sort in reverse (descending) order:
$names = ["George", "Allan", "Peter"];
rsort($names);
var_dump($names);
//$names = ["Peter", "George", "Allan"]