0

I'm trying to get the value of a specific key in an array or string format, but I can't seem to figure out how to make it work.

So, this is an example of an array I'm working with, for example, I'm trying to get all the product_id's in a single array or string format.

Like output productIdArray[] = ['34441', '34442' , '34444'];

Tried achieving this using for each but, Is there a better way to do that?

Any help would be appreciated, Thanks!

(
    [0] => ( Array
                (
                    [id] => 1333708
                    [abc_id] => 429084
                    [test_id] => 58291
                    [order_id] => 2222
                    [product_id] => 34441
                )
        )
    [1] => ( Array
                (
                    [id] => 1333708
                    [abc_id] => 429084
                    [test_id] => 58291
                    [order_id] => 2222
                    [product_id] => 34442
                )
        )
     [2] => ( Array
                (
                    [id] => 1333708
                    [abc_id] => 429084
                    [test_id] => 58291
                    [order_id] => 2222
                    [product_id] => 34444
                )
        )
)

1 Answer 1

1

Simply use array_column

$res = array_column($arr,'product_id');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Lessmore. It worked like a charm.

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.