首页 > 解决方案 > 如何在 Laravel 中按键获取多维数组的值?

问题描述

例如,假设您有一个包含多个键(和值)的常量多维数组,但您想用它的值过滤掉特定的键。请参阅下面的示例数组:

const defaultInvestmentFields = [
     [
         'type' => 'system',
         'investment_name' => 'Ballast'
     ],
     [
         'type' => 'system',
         'investment_name' => 'Inverters'
     ],
        [
         'type' => 'system',
         'investment_name' => 'Extra garantie inverters'
     ]
];

我想要的输出是一个只有投资名称值的数组。喜欢['Ballast', 'Inverters', 'Extra garantie inverters']

标签: phparrayslaravel

解决方案


除了@Levi 's answer之外,您还可以使用数组助手来避免将数组转换为集合并返回:Arr::pluck()

Arr::pluck(Project::defaultInvestmentFields, 'investment_name');

推荐阅读