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.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
kpriyanka
  • 27
  • 8

1 Answers1

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;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 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. – kpriyanka Jun 08 '19 at 08:07
  • 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; } – kpriyanka Jun 08 '19 at 08:09
  • 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 – kpriyanka Jun 08 '19 at 08:27
  • Unfortunately I don't think there is a way of stopping on 1st match unless you throw an exception. https://stackoverflow.com/questions/17853113/break-array-walk-from-anonymous-function shows how if you want to have a look. – Nigel Ren Jun 08 '19 at 08:29
  • OK. Thanks ill go through the link. – kpriyanka Jun 08 '19 at 08:35