Contact Us

array_udiff()

Following on from our previous posts showing us lots about the array_filter() function are are now going to look at another function from the same family called array_udiff().

array_udiff()

The array_udiff() function compares 2 or more arrays and returns elements from the first array based on a user defined function.

If the value should be included in our outputted array we must return the value 1 (for true) and if not we should return the value 0 (for false).

The syntax is as follows:

array_udiff( array1,  array2, array3, function );

We pass in our arrays first and then end with the name of our user defined function. Below is a basic example of this:

function udiffCompare( $value, $value2 )
{

if( $value === $value2 ) { return 1; } else { return 0; }

}

$array    =    array(

                    ‘value1’ => 1,
‘value2’ => 2,
‘value3’ => 3

                );

$array2    =    array(

                    ‘value1‘ => 1,
value2‘ => 2,
value3‘ => 3,
value4‘ => 4,
value5‘ => 5

                );

print_r( array_udiff( $array, $array2, ‘udiffCompare’ ) );

The output of the above would be:

Array
(
    [value1] => 1
    [value2] => 2
    [value3] => 3
)

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

All our udiffCompare() function does is check if each arrays value passed to it are equal 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 only similar values in the arrays are for the value “2” and as such this is all that is returned to 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