1

I'm very new to PHP so please go easy on me. This problem has had me bemused for a couple of hours now so I thought I would swallow my pride and come to you guys in hope that you can see why this could be a problem.

I have an array which I have converted from an object array with the following code:

$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.

This outputs as the following when using 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
                                        (
                                        )

                                )

                        )

                )

        )

)

When I am testing for if a string exists within this array using in_array(), the result is telling me that it does exist within the string, despite it not doing. Please see the code below for this:

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

This outputs:

The string is in the array

But it isn't (as you can see from the array output above).

Thank you in advance for any insight you can offer this new guy :)

1
  • 1
    Try using strict type comparison nl1.php.net/in_array, currently 'string == 0' Commented Dec 14, 2016 at 17:34

1 Answer 1

1

I suspect there is some type coercion going on. When php has to compare the string Chanel with an integer, it does some type conversions to arrive at the result. It seems you would rather this not happen. Fortunately, there is a 3rd optional argument to in_array() which controls whether the types must match as well as the values.

Try changing:

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

to

if (in_array($string, $category_list, true)) {
Sign up to request clarification or add additional context in comments.

4 Comments

This now works if $string is not in the array but after testing for a positive result, it seems that it's only working if I change the value of $string to Root Catalog which exists in the top level of the array output from my post. I tested with Designer which is further nested and was told that it does not exist within the array. Is it possible that it cannot access the nested arrays here? Should I possibly look at an alternative method of converting the initial object array or find another way around this? Thank you.
Maybe something to do with the stdClass Objects of those? Do I possibly need to convert to array twice?
in_array() doesn't recurse into nested arrays. Write a recursive function to do that yourself.
Thanks, I'll look into that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.