首页 > 解决方案 > 借助“帮助”功能创建键 => 值数组

问题描述

我有一个具有以下值的二维数组:

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

标签: phparraysfunction

解决方案


它的第三个参数array_column

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

推荐阅读