php如何从关联数组中获取特定键的值并转为一维数组?
百科、经验、问答,小黄鸡全都有!

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

IT编程qingyu2020-12-10 6:10:502010A+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');

首页 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by 网站地图 关于我们| 留言建议|