DEV Community

Tommy
Tommy

Posted on

Some handy PHP Array functions

Hey buddy, here are some handy-dandy PHP Array functions for you.

Let's declare an example array

$languages = ["php", "javaScript", "html"];
Enter fullscreen mode Exit fullscreen mode

Sorts the array in ascending order.

sort($languages);
Enter fullscreen mode Exit fullscreen mode

Reverse sorts the array in descending order.

rsort($languages);
Enter fullscreen mode Exit fullscreen mode

Sorts the array by keys in ascending order.

ksort($languages);
Enter fullscreen mode Exit fullscreen mode

Reverse sorts the array by keys in descending order.

krsort($languages);
Enter fullscreen mode Exit fullscreen mode

Sorts the array using a natural case-insensitive algorithm.

natcasesort($languages);
Enter fullscreen mode Exit fullscreen mode

Combines two arrays, using the first as keys and the second as values.

array_combine($languages, [1, 2, 3]);
Enter fullscreen mode Exit fullscreen mode

Merges two or more arrays into a single array.

array_merge($languages, [1, 2, 3], [4, 5, 6]);
Enter fullscreen mode Exit fullscreen mode

Searches for an element in an array and returns its index position.

array_search("PHP", $languages);
Enter fullscreen mode Exit fullscreen mode

Checks if an element exists in an array, returning a boolean value.

in_array("php", $languages);
Enter fullscreen mode Exit fullscreen mode

Checks if a key exists in an array.

array_key_exists(0, $languages);
Enter fullscreen mode Exit fullscreen mode

Adds an element to the end of an array.

array_push($languages, "java");
Enter fullscreen mode Exit fullscreen mode

Adds an element to the beginning of an array.

array_unshift($languages, "vue");
Enter fullscreen mode Exit fullscreen mode

Removes the last element from an array.

array_pop($languages);
Enter fullscreen mode Exit fullscreen mode

Removes the first element from an array.

array_shift($languages);
Enter fullscreen mode Exit fullscreen mode

Adds or removes elements from an array at a specified position. (see optional params in the docs below)

array_splice($languages, 0, 1);
Enter fullscreen mode Exit fullscreen mode

Extracts a portion of an array starting from a specified position. (see optional params in the docs below)

array_splice($languages, 0, 2, 100);
Enter fullscreen mode Exit fullscreen mode

Removes duplicate values from the array.

array_unique($languages);
Enter fullscreen mode Exit fullscreen mode

Returns all keys of the associative array.

array_keys($languages);
Enter fullscreen mode Exit fullscreen mode

Returns all values of the associative array.

array_values($languages);
Enter fullscreen mode Exit fullscreen mode

Flips string and integer values of an associative array.

array_flip($languages);
Enter fullscreen mode Exit fullscreen mode

Shuffles the order of the elements of the array.

shuffle($languages);
Enter fullscreen mode Exit fullscreen mode

Array pointer helper functions
Moves the array's internal pointer to the last element.

end($languages);
Enter fullscreen mode Exit fullscreen mode

Returns the current element's value.

current($languages); 
Enter fullscreen mode Exit fullscreen mode

Moves the array's internal pointer to the previous element.

prev($languages);
Enter fullscreen mode Exit fullscreen mode

Moves the array's internal pointer to the next element.

next($languages);
Enter fullscreen mode Exit fullscreen mode

Got more? Feel free to add in the comment section below.

Doc
https://www.php.net/manual/en/ref.array.php

Top comments (0)