【问题标题】:Create array of key => value thanks to "helper" functions借助“帮助”功能创建键 => 值数组
【发布时间】:2020-08-04 08:07:58
【问题描述】:

我有一个包含这个值的二维数组:

  [
    'id' => 12,
    'title' => 'the title', //and a few other key => value

    ],
        [
    'id' => 13,
    'title' => 'the title 13', // and a few other key => value
    ],...

最后,我需要一个只有id和title

的多维数组

[ $item['id'] => $item['title'], ...]

通常,我正在做一个简单的 foreach 来实现这一点,但我现在想使用 php 函数。我已经这样做了,但是有没有合适的方法来做到这一点?

$list = array_combine(array_column($list_forms, 'id'), array_column($list_forms, 'title'));

【问题讨论】:

    标签: php arrays function


    【解决方案1】:

    array_columnthird argumenf 是:

    $list = array_column($list_forms, 'title', 'id');
    

    【讨论】:

    • 谢谢,太好了!没注意array_column中的第三个参数
    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2013-12-08
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多