2

I've decoded my JSON string and now I want to use the in_array() function in PHP to check if one of the ID numbers is present in the array (number 5 to be exact).

Array:

Array ( [0] => Array ( [id] => 5 ) [1] => Array ( [id] => 4 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [0] => Array ( [id] => 7 [children] => Array ( [0] => Array ( [id] => 10 ) ) ) ) ) ) ) [2] => Array ( [id] => 3 [children] => Array ( [0] => Array ( [id] => 6 [children] => Array ( [0] => Array ( [id] => 8 ) ) ) [1] => Array ( [id] => 9 ) ) ) )

My code (that's failing):

if (in_array(5, $array)) {
    echo "i'm in the array!";
}

or

if (in_array('5', $array)) {
    echo "i'm in the array!";
}

This seems like a simple problem - am I blind...

ABOO
  • 145
  • 1
  • 8
  • I might not be really sharp but you have an array containing multiple arrays and you expect of in an integer in it? – Sven van de Scheur May 19 '14 at 15:23
  • 1
    Not blind. `in_array` does not check recursively through arrays. Why don't you look at the recursive function answers [from your previous question](http://stackoverflow.com/questions/23739215/format-my-json-string-into-an-ol-ordered-list-in-php/23740643#23740643) and adapt them to look for a value of 5 instead of kicking out an `
      `.
    – Crackertastic May 19 '14 at 15:25
  • @SvenvandeScheur perhaps i'm asking too much. I was under the impression that in_array() would be able to find an integer as well as strings. – ABOO May 19 '14 at 15:26
  • @Crackertastic thanks for the advice - this issue is related to a slave list that shows all of the items that are NOT in my previous
      – ABOO May 19 '14 at 15:27
    1. @ABOO It can find either, but it won't look inside of arrays that are in other arrays. In other words, it begins scanning whatever array you give it for a value of `5`, but when it encounters a value that is another array it continues as if it did not find the value `5` - it found the value `array` and `array != 5`. – Crackertastic May 19 '14 at 15:28

    3 Answers3

    2

    (not tested)(even can search sub arrays also (from php))

    if(recursive_array_search("5",$array))
    {
    echo 'found';
    }else
    {
    echo 'not found';
    }
    
    function recursive_array_search($needle,$haystack) {
        foreach($haystack as $key=>$value) {
            $current_key=$key;
            if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
                return $current_key;
            }
        }
        return false;
    }
    
    ɹɐqʞɐ zoɹǝɟ
    • 4,342
    • 3
    • 22
    • 35
    1

    You have more than one dimension in the array. You either need to loop through and check id or use array_column if you have PHP >= 5.5:

    $ids = array_column($array, 'id');
    if(in_array(5, $ids)) {
        echo "i'm in the array!";
    }
    

    Or:

    foreach($array as $value) {
        if($value['id'] == 5) {
            echo "i'm in the array!";
            break;
        }
    }
    
    AbraCadaver
    • 78,200
    • 7
    • 66
    • 87
    0

    The value you are looking for is not 5 but array("id" => 5) does that help your understanding?

    I'm not sure if in_array(array("id" => 5), $array) works but if it does, great!

    Otherwise, use a typical find-pattern algorithm to match it: loop over values looking for a match, return early if you can.


    You can also consider re-indexing your input array so id-values are more easily found.

    Halcyon
    • 57,230
    • 10
    • 89
    • 128