PHP in_array()带回不正确的结果

时间:2016-12-14 17:26:50

标签: php arrays

我对PHP非常陌生,所以请放轻松我。这个问题让我困惑了好几个小时,所以我想我会吞下自己的骄傲来找你们,希望你能明白这可能是个问题。

我有一个数组,我已使用以下代码从对象数组转换:

$category_tree = $client->catalogCategoryTree($session_id); // Get an object array of all categories and assign to $category_tree.
$category_list = (array) $category_tree; // Convert $category_tree into an array and assign to $category_list.

使用print_r($category_list)时输出如下:

Array
(
    [category_id] => 1
    [parent_id] => 0
    [name] => Root Catalog
    [position] => 0
    [level] => 0
    [children] => Array
        (
            [0] => stdClass Object
                (
                    [category_id] => 2
                    [parent_id] => 1
                    [name] => Root Category
                    [is_active] => 1
                    [position] => 1
                    [level] => 1
                    [children] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [category_id] => 8
                                    [parent_id] => 2
                                    [name] => Designer
                                    [is_active] => 1
                                    [position] => 1
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                            [1] => stdClass Object
                                (
                                    [category_id] => 7
                                    [parent_id] => 2
                                    [name] => Shop Bags
                                    [is_active] => 1
                                    [position] => 2
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                            [2] => stdClass Object
                                (
                                    [category_id] => 5
                                    [parent_id] => 2
                                    [name] => DifferentCategory
                                    [is_active] => 1
                                    [position] => 3
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [category_id] => 6
                                                    [parent_id] => 5
                                                    [name] => 1
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [3] => stdClass Object
                                (
                                    [category_id] => 4
                                    [parent_id] => 2
                                    [name] => Sample Category
                                    [is_active] => 1
                                    [position] => 6
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

当我使用in_array()测试此数组中是否存在字符串时,结果告诉我它确实存在于字符串中,尽管它没有这样做。请参阅以下代码:

$string = 'Chanel';
if (in_array($string, $category_list)) {
    echo 'The string is in the array';
} else {
    echo 'The string is not in the array';
}

输出:

The string is in the array

但它不是(正如你从上面的数组输出中看到的那样)。

提前感谢您提供有关此新人的任何见解:)

1 个答案:

答案 0 :(得分:1)

我怀疑是否存在某种强制类型。当php必须将字符串Chanel与整数进行比较时,它会进行一些类型转换以获得结果。看来你宁愿这不会发生。幸运的是,in_array()有一个第三个可选参数,它控制类型是否必须与值匹配。

尝试更改:

if (in_array($string, $category_list)) {

if (in_array($string, $category_list, true)) {