简体   繁体   English

in_array找不到值

[英]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? 我究竟做错了什么?

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 : PS:如果要吸收整数和字符串,请删除true参数:

123 or "123" 123或“ 123”

you have declared a 'strict' check on in_array . 您已对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. in_array将仅检查提供的数组的直接值。 Have a look at this question for a possible solution to that part of the problem. 看一下这个问题,为该部分问题寻求可能的解决方案。 (Or Delphines answer) (或Delphines回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM