Hey buddy, here are some handy-dandy PHP Array functions for you.
Let's declare an example array
$languages = ["php", "javaScript", "html"];
Sorts the array in ascending order.
sort($languages);
Reverse sorts the array in descending order.
rsort($languages);
Sorts the array by keys in ascending order.
ksort($languages);
Reverse sorts the array by keys in descending order.
krsort($languages);
Sorts the array using a natural case-insensitive algorithm.
natcasesort($languages);
Combines two arrays, using the first as keys and the second as values.
array_combine($languages, [1, 2, 3]);
Merges two or more arrays into a single array.
array_merge($languages, [1, 2, 3], [4, 5, 6]);
Searches for an element in an array and returns its index position.
array_search("PHP", $languages);
Checks if an element exists in an array, returning a boolean value.
in_array("php", $languages);
Checks if a key exists in an array.
array_key_exists(0, $languages);
Adds an element to the end of an array.
array_push($languages, "java");
Adds an element to the beginning of an array.
array_unshift($languages, "vue");
Removes the last element from an array.
array_pop($languages);
Removes the first element from an array.
array_shift($languages);
Adds or removes elements from an array at a specified position. (see optional params in the docs below)
array_splice($languages, 0, 1);
Extracts a portion of an array starting from a specified position. (see optional params in the docs below)
array_splice($languages, 0, 2, 100);
Removes duplicate values from the array.
array_unique($languages);
Returns all keys of the associative array.
array_keys($languages);
Returns all values of the associative array.
array_values($languages);
Flips string and integer values of an associative array.
array_flip($languages);
Shuffles the order of the elements of the array.
shuffle($languages);
Array pointer helper functions
Moves the array's internal pointer to the last element.
end($languages);
Returns the current element's value.
current($languages);
Moves the array's internal pointer to the previous element.
prev($languages);
Moves the array's internal pointer to the next element.
next($languages);
Got more? Feel free to add in the comment section below.
Top comments (0)