1

I have a multidimensional array containing child arrays in the following format:

[0] Array =>
    (
        [first] => Foo
        [second] => Bar
    )
[1] Array =>
    (
        [first] => Foo
        [second] => Bar
    )
[2] Array =>
    (
        [first] => Foo
        [second] => bingo
    )
[3] Array =>
    (
        [first] => jackpot
        [second] => bar
    )

I would like to search the entire array for 'bingo' and 'jackpot' and remove any child arrays that do not contain these values (in the above example, array 0 and array 1 should be removed).

I understand how to search the array using array_search('bingo', $myarray) but not how to remove the other two. Is there a simple way to achieve this?

4
  • 1. In your title you have if doesn't exists, so I'm a bit confused 2. and remove the first two element (0, 1) You mean the first 2 arrays or what? Commented Apr 6, 2015 at 14:04
  • Sorry, I've amended title.... in the example, I would like to remove array 0 and array 1 as these two do not contain either 'bingo' or 'jackpot'. Commented Apr 6, 2015 at 14:07
  • Ah so your pattern is either "jackpot" or "bingo" has to be in the array otherwise you want to remove it? (BTW: Add your attempt in your question! No matter if it worked or so just add what you tried, like with array_search or so) Commented Apr 6, 2015 at 14:08
  • Yes, exactly, so as array 0 and array 1 do not contain this pattern, they should be removed from the parent array... sorry, I am very new to this. Commented Apr 6, 2015 at 14:11

1 Answer 1

3

This should work for you:

(Here I just filter all arrays out with array_filter() which does have an element with either jackpot or bingo in it, so only the arrays which doesn't have either jackpot or bingo in it will remain. After this I get all keys of these arrays with array_keys() and loop through them and unset they arrays)

<?php

    $keys = array_keys(array_filter($arr, function($v, $k){
        if(in_array("jackpot", $v) || in_array("bingo", $v) )
            return FALSE;
        return TRUE;
    }, ARRAY_FILTER_USE_BOTH));

    foreach($keys as $key)
        unset($arr[$key]);

    print_r($arr);

?>

output:

Array
(
    [2] => Array
        (
            [first] => Foo
            [second] => bingo
        )

    [3] => Array
        (
            [first] => jackpot
            [second] => bar
        )

)

EDIT:

Even a simpler solution would be just to do this:

foreach($arr as $k => $v) {
    if(!in_array("jackpot", $v) && !in_array("bingo", $v))
        unset($arr[$k]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Great thanks, but I seem to be getting an error from ARRAY_FILTER_USE_BOTH : "Notice: Use of undefined constant ARRAY_FILTER_USE_BOTH - assumed 'ARRAY_FILTER_USE_BOTH'"
@user2726041 echo phpversion(); = ... ? <5.6?
Ah! I am using 5.4.10
@user2726041 updated it again! Just refresh the page again and under edit this should do everything! Just print the array after it and you get your expected results

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.