Some cool Array functions in PHP

When ever you have an array and you have to find an element or filter an array for different situation most of the time this is achieved by writing loop. Some times its okay to write loop. But php offers some nice set of array functions which can help you to achieve your goal without writing a loop.

Let say we have an array for a list of products.

If we want to increment each product price by 10%, we can achieve this by using array_map function.

array map

you can do var_dump new product price have 10% more than previous one.

If i have to look for an item having category is mobile, array_filter can help here


If i would like to sum all products price array_reduce work out of box.

In array_reduce function when callback function iterate over first item, $current_iteration_value is 0.

array_chunk is a function which i like most, i achieved pagination using this function.
It split array in to number of chunks.

Let say if we have numbers of products to display, where each page have only 22 products to display and whole products array have 500 products. You can achieve this by array_chunk

There are other good array_functions available in php which you can see here.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s