简体   繁体   English

in_array function 不工作

[英]in_array function is not working

This code should return TRUE value:此代码应返回TRUE值:

<?php
      $return = in_array(array(1, 2), array(1, 2));
?>

but in_array returns FALSE.in_array返回 FALSE。

in_array checks if a value exists in an array. in_array检查数组中是否存在值。

Your $needle doens't exists at all as a value of $haystack您的$needle作为$haystack的值根本不存在

that would be ok if your $haystack was如果你的$haystack

array(1,2,3,array(1,2))

Notice in this case array(1,2) actually is found inside as expected请注意,在这种情况下, array(1,2)实际上是按预期在内部找到的

If you want to check whenever 2 arrays are equal i suggest you the === operator如果您想检查 2 arrays 是否相等,我建议您使用===运算符

($a === $b) // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

Based on your example, you may want to look into array_intersect() .根据您的示例,您可能需要查看array_intersect() It compares arrays in a fashion that may better align with your spec.它以更符合您的规格的方式比较 arrays。

According to the PHP Manual for in_array , the function's syntax is:根据in_array的 PHP 手册,函数的语法是:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

So you need to supply a $needle value as the first argument.所以你需要提供一个$needle值作为第一个参数。 This explains why your example returns FALSE.这解释了为什么您的示例返回 FALSE。 However, these examples will each return TRUE:但是,这些示例都将返回 TRUE:

in_array(1, array(1, 2));
in_array(2, array(1, 2));
in_array(array(1, 2), array(1, 2, array(1, 2)))

That said, it might help if you explain exactly what you are trying to do.也就是说,如果您准确地解释您要做什么,它可能会有所帮助。 Perhaps in_array is not the function you need.也许in_array不是您需要的 function。

Your first array isn't contained in the second array, it's equal.您的第一个数组不包含在第二个数组中,它是相等的。

This returns true:这返回真:

var_dump(in_array(array(1, 2), array(1, 2, array(1, 2))));

Are you interested in intersection ?你对路口感兴趣吗?

$arr1 = array(1, 2);
$arr2 = array(1, 2);

$return = array_intersect($arr1, $arr2);

if(count($return) === count($arr1)) {
    // all are present in arr2
}

First parameter is the value you're looking for in the second parameter (array) http://php.net/manual/fr/function.in-array.php第一个参数是您在第二个参数(数组)中查找的值http://php.net/manual/fr/function.in-array.php

you missunderstand in_array see offiziell docs: http://uk.php.net/in_array你误解了 in_array 见官方文档: http://uk.php.net/in_array

<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was found\n";
}

if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was found\n";
}

if (in_array('o', $a)) {
    echo "'o' was found\n";
}
?>

array(1,2) is not in array(1,2) it is array(1,2),数组(1,2)不在数组(1,2)中,它是数组(1,2),

$return = in_array(array(1, 2), array(array(1, 2)));

would return true.将返回 true。 (more an extension of yes123's answer) (更多 yes123 答案的扩展)

In your case, the first parameter of in_array should not be an array, but an integer.在您的情况下,in_array 的第一个参数不应该是一个数组,而是一个 integer。 What you are doing with that code is checking for the presence of an array inside the array, which is not there.您正在使用该代码执行的操作是检查数组中是否存在数组,而该数组不存在。 A correct form would be:正确的形式是:

in_array(1, array(1, 2)); // true

if second array looks like this如果第二个数组看起来像这样

array(array(1, 2));

then return true然后返回真

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

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