PHP - 从关联数组中获取特定键的值?
PHP - Fetch the values of a specific key from an associative array?
我试图以数组或字符串格式获取特定键的值,但我似乎无法弄清楚如何让它工作。
所以,这是我正在使用的数组示例,例如,我试图以单个数组或字符串格式获取所有 product_id
。
点赞输出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');
我试图以数组或字符串格式获取特定键的值,但我似乎无法弄清楚如何让它工作。
所以,这是我正在使用的数组示例,例如,我试图以单个数组或字符串格式获取所有 product_id
。
点赞输出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');