简体   繁体   中英

PHP - Fetch the values of a specific key from an associative array?

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
                )
        )
)

Simply use array_column

$res = array_column($arr,'product_id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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