繁体   English   中英

PHP - 从关联数组中获取特定键的值?

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

我试图以数组或字符串格式获取特定键的值,但我似乎无法弄清楚如何使其工作。

因此,这是我正在使用的数组的一个示例,例如,我试图以单个数组或字符串格式获取所有product_id

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

尝试使用 for each 但是,有没有更好的方法来做到这一点?

任何帮助将不胜感激,谢谢!

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

只需使用array_column

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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