These PHP array functions will either slow you down or accelerate your success as a PHP programmer.
I am frequently reaching out to these most common PHP array functions. The most annoying thing is that I have to google their syntax every now and then. Nowadays, my IDE helps me avoid that. Still, it is frustrating to keep looking into the docs.
For instance, if you compare array_slice and array_splice, you would know what I mean. Both are very similar albeit crucially different. However, their syntax is completely unrelated.
Anyways, enough with the complaining. What can I do to fix this problem for myself and hopefully for anyone reading this blog? I thought can write up the most crucial array functions.
So, here we go.
- array() to create an array,
- sizeof() or count() to get the size of an array,
- range() to create a range of PHP values,
- array_keys() and array_values(),
- array_slice() to slice your arrays into smaller once,
- The explode(),
- The extract() function to get variables from array,
- The in_array() function to see if an element is in an array,
- The is_array() function to check if a variable is an array,
- The array_merge function to combine arrays into one big one,
- The array_push() and array_pop() functions to add and remove elements from the end of an array,
- The array_shift() and array_unshift() to add and remove element from the start of the array,
- Sorting functions,
- Functions that act on all arrays.
1. The array()
The array() function creates an array just like the square syntax. You can use the one that you find easy.
$days = array(1 => "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); // 2 is Tue, 3 is Wed, etc.
2. The sizeof() or count() functions
These two functions do the same job. They just return the size of the array. It is better to stick with one.
3. range() to create an array of values in a range
The range function is cool when you are in a hurry and you want to quickly generate an array of values. For instance, if you want an array containing the numbers from 5 all the way to 11, just do.
range(5, 11); // will give us an array [5, 6, 7, 8, 9, 10, 11]
3. The array_slice()
The array slice is the best of the bunch. It is a PHP array function that we can use to slice a bigger array into smaller parts. It takes three arguments, the original array, the starting index, and the length of the new array. Then, it produces the new array of the given length starting from the beginning index.
One thing to note is that array_slice doesn’t mutate your original array. Hence, the result has to be assigned to a new variable. Here is an example:-
$names = ["Abebe", "Tigist", "Belay", "Mamo"]; $fewNames = array_slice($names, 1, 2); //Will give us the array ["Tigist", "Belay"] $fewNames = array_slice($names, 1); //Will give us the array ["Tigist", "Belay", "Mamo"]
4. The array_keys() and array_values() PHP array functions
These two PHP array functions are great when you just want the keys or the values of an associative array. And those situations are way common than you think.
$employees = ["emp1" => "Abebe", "emp2" => "Tigist", "emp3" => "Belay"]; array_key_exists("emp2", $employee);//Will return true, as there is an employee with key of emp2 array_key_exists("emp4", $employee);//Will return false, as there is no employee with key of emp4
5. The explode() function
The explode function, technically not an array function, just gives you an array from a string. It is a great tool to have if you are working with CSV. Normally, once you load the CSV, you just have a big string. You can simply convert it into an array by using explode.
By the way, you can do the reverse using the implode.
6. The extract() function
Most of the time, you want the result of a database query or a JSON decode to be an array. Almost always, the result of the fetch or decoding is an associative array. Mapping each of these array values into a variable could be tedious. That is where extracts come into play.
Extract assigns each value of an associative array into variables made out of the keys.
$employees = ["name" => "Abebe", "department" => "finance", "salary" => 3400]; extract($employees);//Now we have variables $name, $department, adn $salary. echo $name;//Will output "Abebe"
7. The in_array() function
The in_array function simply returns either true or false if the first argument is in the array or not. The second argument is the array itself. There is that inconsistency again.
$names = ["Abebe", "Tigist", "Belay", "Mamo"]; echo(in_array("Abebe", $names));//is true echo(in_array("Helllo", $names));//is false
8. The is_array() function
It simply checks if a given variable is an array or not. It returns either true or false.
9. The array_merge() function
The array_merge() function is awesome to have when you want one big array from smaller arrays.
$group1 = ["A", "B", "C"]; $group2 = ["D", "E"]; $allgroups = array_merge($group1, $group2);//["A", "B", "C", "D", "E"]
10. The array_push() and arra_pop() functions
The array_push() function can help us to easily add an element into an array. Whereas, the array_pop() function does the opposite by removing and returning the last element.
11. The array_shift() and array_unshift() functions
If you want to add and/or remove an element from the beginning of an array, use the shift and unshift functions.
12. The array_walk() function
The array walk function is great for iterating over an array and applying a function to each element. It is like JS’s map.
I will write more on array_walk soon. Until then, I will go back and forth to the PHP array_walk function documentation.
13. PHP array sorting functions
PHP gives us a lot of easy-to-use array sorting functions. Some of these functions are sort(), asort(), ksort(), etc.
Just like array_walk, I want to get a good grip and detailed knowledge of sorting before I can jot my notes here. Until then, the official doc on array sorting does a good job.
14. PHP array functions that act on all the arrays.
Again this group is a vast one. I will have to take a deep dive and come back.
Maybe I have missed some, but these are the most common and most important PHP array functions that I repeatedly come back to.
I am re-learning PHP and PHP array functions because I believe they can improve my skill. If you think they should be relearned, please let me know and I will go into detail into each of them.