0

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
    )

5 Answers 5

2

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 ] );  
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Use in_array.

$array_to_filter = array(array(...));
$array_filter    = array(...);
foreach ($array_to_filter as $key => $value) {
    if (in_array($value['customer'], $array_filter)) {
        unset($array_to_filter['$key']);
    }
}

1 Comment

This answer is missing its educational explanation.
0
$i = 0;
for ($i = 0; $i <count($array1) ; $i++) {

   if ($array1[$i]['customer'] == $array2[0] || $array1[$i]['customer'] == $array2[1]) {

       unset($array[$i]);

       // restart
       $i = 0
   }
}

1 Comment

This answer is missing its educational explanation.
0

Try:

$types = array('Engineering', 'Painting');

foreach( $array as $key => $v ) {
    if( in_array($v['customer'], $types ) ) {
        unset($array[$key]);
    }
}

Comments

0

You could try:

$arrayBlackList = array('Engineering','Painting');

function shouldKeep($var)
{
    global $arrayBlackList;
    return !in_array($var['customer'],$arrayBlackList);
}

$filtered_array = array_filter($array1, "shouldKeep");

1 Comment

This answer is missing its educational explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.