Contact Us

Array Filter array_filter()

Following on from our previous posts showing us lots of lovely PHP Array functions , in this post we are going to delve further into creating our own custom filters for our arrays using array_filter().

 

array_filter()

The array_filter() function is useful when wanting to use your own function to filter an array based on its values.

The syntax is as follows:

array_filter( array, callbackFunction );

We pass our array in as the first parameter and then the name of our custom function as the second. Below is a basic example of this:

function isNumber( $value )
{

if( is_numeric( $value ) ) { return( $value ); }

}

$array    =    array( 

‘value1’ => ‘a’,
‘value2’ => ‘b’,
‘value3’ => 2,
‘value4’ => 3,
‘value5’ => 4            );

print_r( array_filter( $array, ‘isNumber’ ) );

The output of the above would be:

Array ( [value3] => 2 [value4] => 3 [value5] => 4 )

So, what is the above doing? Firstly our array is passed into the array_filter() function, which in turn passes it to our custom callback function isNumber() which we defined above.

All our isNumber() function does is check if each value passed to it is a numeric value or not and if it is it returns it as part of a new array, maintaining its array key value.

In the instance above you can see that the first two values of a and b are not numeric so are ignored, however the values 2, 3 and 4 are numeric and therefore form part of our new array.

The beauty here is that you can make the call back function as simple or as complicated as your heart desires and is a great way to filter out our arrays in a way that other functions might just not accomplish.

Hopefully these functions will help you when filtering your arrays in your PHP Applications. If you would like any help with your PHP development or web projects then why not get in contact with us here at Network Intellect.

This article was posted by



Leave a Reply

What our clients say

"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
"They have always been professional and focused on delivering results in a timely and cost effective manner. It is enlightening to have an organisation that takes the time to make recommendations on actual data analysis."
Business Manager, Charity
"They (Network Intellect) continue to exhibit their digital expertise by helping us to establish a competitive advantage by pushing the boundaries and innovating their technology and performance solutions."
Head of Marketing Communications, Education
"Because of their talent, expertise and level of service our on-line business has increased dramatically."
Founder, Electronics
"I would have no hesitation in recommending them as an analytics partner to understand and improve the performance of a website."
Vice President, Retail
Network Intellect continue to give us a fantastic service, fast and friendly and are always ready to go the extra mile. They have gone above and beyond the call of duty to deliver our results.
Director, E-Cigarette Web
"We found that Network Intellect had the depth of knowledge we needed and after speaking to them a few times we decided to go with them. It’s the best decision we’ve made in some time."
Owner, Retail
"Their services offered great value for money. I would not hesitate to recommend them as a digital partner to others."
Strategic Marketing Manager, Education
"Jason and his team at Network Intellect embody efficiency and efficacy. I would have no hesitation in recommending this excellent company to others."
Marketing Manager, Education
Network Intellect have managed our product data feeds with exceptional care and meticulous detail. I know I can trust them to make the right decisions for our business."
Director, E-Commerce
"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
Read all testimonialsRead all testimonials

We can help you be great

We have friendly staff ready to help you get on your path to greatness...
Network Intellect Menu