Manish Jesani
Manish Jesani

Reputation: 1347

php compare multidimensional array values in single dimensional array

I have two different array. One is single dimensional array like below.

Array
( 
   [0] => products_01.jpg
   [1] => products_02.jpg 
) 

And Second array is multi dimensional array like below.

Array
(
    [0] => Array
        (
            [imgurl] => http://example.com/upload/products_01.jpg
            [name] => products_01.jpg
        )

    [1] => Array
        (
            [imgurl] => http://example.com/upload/products_02.jpg
            [name] => products_02.jpg
        )

    [2] => Array
        (
            [imgurl] => http://example.com/upload/products_03.jpg
            [name] => products_03.jpg
        )

    [3] => Array
        (
            [imgurl] => http://example.com/upload/products_04.jpg
            [name] => products_04.jpg
        )

)  

Now i want to compare both array and if we get same value for key "name" then delete that array. Without using foreach or for. Do any one have idea that php provide any inbuilt array function or not ?

I want Output Like Below

Array
(

    [0] => Array
        (
            [imgurl] => http://example.com/upload/products_03.jpg
            [name] => products_03.jpg
        )

    [1] => Array
        (
            [imgurl] => http://example.com/upload/products_04.jpg
            [name] => products_04.jpg
        )

)  

Upvotes: 0

Views: 1431

Answers (1)

Ben Plummer
Ben Plummer

Reputation: 459

Assuming the single dimensional array is called $arrayA and the multi-dimensional array is called $arrayB, you could do the following:

$arrayB = array_filter($arrayB, function($arrayBItem) use ($arrayA) {
    return ! in_array($arrayBItem['name'], $arrayA); 
});

If you don't want to override the multi-dimensional array, $arrayB, assign the array_filter call to a different variable instead e.g. $arrayC.

Hope this helps!

Upvotes: 4

Related Questions