0

I have a array in below format

Array ([abc] => Array ( [0] => Array ( [abc_id] => 10 [status] => true ) [1] =>
Array ( [abc_id] => 11 [status] => true ) [2] => Array ( [abc_id] => 12 [status] => true ) ) 
[pqr] => Array ( ) [xyz] => Array ( [0] => Array ( [xyz_id] => 8 [status] => false )
[1] => Array ( [xyz_id] => 9 [status] => false ) ) [mno] => Array ( ) [def] => Array ( ) [unit_id] => 1)

And I want to check if there is a status = false word in the entire array. I tried using array_in but couldn't succeed. Can anyone give me some proper solution for the above issue? Is there any other way to check if the array includes false anywhere.

1 Answer 1

1

You can use array_walk_recursive() which will go through all levels of a multidimensional array - BUT only the leaf nodes. Although that is fine as that is what you want.

This code has a found flag and a simple function which checks the label and the value for the values your after and sets the flag to be true if they both match...

$found = false;
array_walk_recursive($aray, function ($item, $key) use (&$found) {
    if ( $key === "status" && $item == false) {
        $found = true;
    }
});
echo $found;
Sign up to request clarification or add additional context in comments.

5 Comments

Hi sir @Nigel Ren your code is working,only had to replace '==' with '=== ' in if condition.I have made some changes according to my requirement ie. i have to return a value depending on if condition when condition is true i have send success and if false the failed. so i used else part also.but when i use else part it works as follows hi : hello :1 hi :1.
Here is code with changes : $found = false; array_walk_recursive($output, function ($item, $key) use (&$found) { if ( $key === "status" && $item == "false") { $found = true; echo "hello :".$found; } else { echo "hi :".$found; }
Yes done.Sir is it possible to stop if condition once it gets 1st false value.I mean if there are 3 false values in whole array than I want to give message as failed once it matches with 1st false .and if there is no false in whole array than success
Unfortunately I don't think there is a way of stopping on 1st match unless you throw an exception. stackoverflow.com/questions/17853113/… shows how if you want to have a look.
OK. Thanks ill go through the link.

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.