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