php如何从关联数组中获取特定键的值并转为一维数组?

php如何从关联数组中获取特定键的值并转为一维数组?

C++qingyu2020-12-06 18:14:461322A+A-

  php如何从关联数组中获取特定键的值并转为一维数组?

php如何从关联数组中获取特定键的值并转为一维数组?

  问题描述:

  我想从关联数组中获取特定键的值,但不知道具体代码怎么写。

  我把关联数组的贴出来,比如我想获取以数组键为product_id的值,并把它们转为一维数组。

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

  像这样输出:

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

  回答:

  只需使用 array_column 函数

  $res = array_column($arr,'product_id');
点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理