user6171196
user6171196

Reputation: 17

in_array does not find value

I have the following array:

Array
(
    [0] => Array
        (
            [video_id] => 161162806
        )

    [1] => Array
        (
            [video_id] => 161736574
        )

    [2] => Array
        (
            [video_id] => 156382678
        )    
)

I try to find a value, but even though it's in the array it doesn't find it.

if(in_array("161162806", $safe, true)) {      
 echo "approved video";
  } else { 
  echo "non-approved video";
 }

What am I doing wrong?

Upvotes: 0

Views: 560

Answers (2)

DevDonkey
DevDonkey

Reputation: 4880

you have declared a 'strict' check on in_array.

Because of this it will check the type as well. Seeing as your search parameter is a string, it wont match the integer in the array.

try this (when you've sorted the below mentioned issue):

if(in_array("161162806", $safe)) {      
   echo "approved video";
} else { 
   echo "non-approved video";
}

but you have separate arrays in the one your'e checking. in_array will only check the direct values of the array provided. Have a look at this question for a possible solution to that part of the problem. (Or Delphines answer)

Upvotes: 0

Delphine
Delphine

Reputation: 888

It is because you have arrays in array (multidimensional array).

You have to loop over :

foreach($safe as $s) {

if(in_array("161162806", $s)) {      
 echo "approved video";
  } else { 
  echo "non-approved video";
 }
}

PS : Remove true param if you want to assimilate integers and strings :

123 or "123"

Upvotes: 2

Related Questions