Best PHP array functions
During the writing PHP code in process of developing web applications, we use various programming techniques. But i think that there is hard to find any class or even a function which doesn’t manipulate with arrays. Let’s see what is the most useful PHP functions that manipulate with arrays.
array_walk(array &$array, callback $funcname, $username) – function apply any of your functions to every member of an array
Very useful function when you want to do some data processing on each array element.
array_slice ( array $array , $offset , $length) – returns part of array specified by the offset and length parameters.
This really can be helpful when when you want to make sub-array from main array.
array_chunk ( array $input , $size , bool $preserve_keys = false ) – split an array into size large chunks.
Splits one big array into smaller chunks of arrays with same size. Magic function!
array_reverse ( array $array , bool $preserve_keys = false ) – takes an array and reverse its content
This function speak for it self.
array_flip ( array $trans ) – switches keys with values
This won’t be needed you all the time, but it can save you some time when you are in situation to flip keys with values.
array_keys ( array $input ,$search_value ) – get all array keys
From your array this function returns an array which contains keys from main array. Can be very useful!
array_merge ( array $array1 [, array $... ] ) – merge one or more arrays
If you have more then one array, and you want to manipulate with them together, this is probably what you need.
array_fill ( int $start_index , int $num , $value ) – Fill array with parameter on defined positions
With this function you can fill some parts of an array with values that you want.
array_pop ( array &$array ) – Pop last value of an array and returns that array.
Simple function that remove last element of an array, pretty straight forward.
sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) – Sort an array
Sort an array based on sort flags.
This is the most common array manipulation functions that i am using in everyday programming, although i probably forgot to mentioned some function.
If you have any other php array function that you often using that is not on this list, please write comment and notice other users about it.
No related posts.
Hi, i am Vladimir Popov, web developer and CEO of Webmarket, Internet company located in Belgrade. I am passionate about Web technologies and creating new features on the Internet and make it a better place to live in.
Hi Vladimir,
You forgot to mention array_intersect_key
http://php.net/manual/en/function.array-intersect-key.php
Very useful if you are using large amounts of keys and need the difference.
Hi Bear,
Yes, intersection of keys can be useful for big arrays.
Thanks for the great work! Cool blog. There are a number of opinions on this topic and this blog states the problem very good.
Extremely amazing web-site! Cheers for taking the time to create this captivating post!