Search code examples
phparraysmultidimensional-arrayparentunset

Remove element in multidimensional array with match PHP


I have this multidimensional array:

[0] => Array
    (
        [id] => 55829
        [date] => 2014-09-05 07:00:56
        [customer] => Engineering
        [server] => example
        [status] => Successful
        [version] => 1
    )

[1] => Array
    (
        [id] => 55776
        [date] => 2014-09-05 06:58:30
        [customer] => Coating
        [server] => example
        [status] => Successful
        [version] => 1
    )

I want to be able to loop through the array and if the second level 'customer' value matches a value in this array:

Array
(
    [0] => Engineering
    [1] => Painting
)

I then want to remove/unset the parent array completely if there is a match so that the first array would then be:

[0] => Array
    (
        [id] => 55776
        [date] => 2014-09-05 06:58:30
        [customer] => Coating
        [server] => example
        [status] => Successful
        [version] => 1
    )

Solution

  • The below should work where $records is the multi dimensional array and $second_array contains Engineering / Painting.

    <?php
    foreach( $records as $key => $record )
    {
        if( in_array( $record['customer'], $second_array ) )
        {
            unset( $records[ $key ] );  
        }
    }
    ?>