0

I'm trying very simply to use in_array() to check a key is in an array and then echo its value.

$array = Array
( 
    [cart_item] => Array 
        ( 
            [0] => Array 
                ( 
                    [product_name] => White Sakura Necktie
                    [id] => 11
                    [product_auto_id] => 556729685
                    [quantity] => 2
                    [product_regular_price] => 95
                    [product_sale_price] => 95
                    [product_image] => 556729680Black_Sakura_Necktie.jpg 
                )
            [1] => Array 
                ( 
                    [product_name] => hhhad ba bhdbh
                    [id] => 10
                    [product_auto_id] => 951790801
                    [quantity] => 2
                    [product_regular_price] => 20
                    [product_sale_price] => 
                    [product_image] => 951790801hhhad_ba_bhdbh_.jpg 
                )
        ) 
)

And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.

in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);

All above three i have used but result always showing NULL means blank.

I am really frustrated to find the solution. I don't understand what's happening.

0

1 Answer 1

7

You should use array_column() which will return the values from a single column in the input array as an array.

$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');

In this case, it would return the following:

Array
(
    [0] => 556729685
    [1] => 951790801
)

Then you can use in_array() like you currently are.

in_array(556729685, $product_auto_ids);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.