dsj60862 2016-08-05 05:15
浏览 34
已采纳

将多维数组合并到单个数组[重复]

This question already has an answer here:

I have the following array structure

array (
  0 => 
  array (
    'ID' => '1',
    'post_title' => 'Hello world!',
  ),
  1 => 
  array (
    'ID' => '79',
    'post_title' => 'ffffffffffff',
  ),
  2 => 
  array (
    'ID' => '1720',
    'post_title' => 'Git primer',
  ),
)

I will love to convert it to a structure similar to the one below. Is there any php function that can do this? I am trying to avoid repetitive foreach loop.

array (
'1' => 'Hello world!',
'79' => 'ffffffffffff',
'1720' => 'Git primer',
)
</div>
  • 写回答

3条回答 默认 最新

  • douguai7291 2016-08-05 05:23
    关注

    Use array_column()to get this.

    Array_column() function return all column name you have specify in parameter.

    $array=array (
      0 => 
      array (
        'ID' => '1',
        'post_title' => 'Hello world!',
      ),
      1 => 
      array (
        'ID' => '79',
        'post_title' => 'ffffffffffff',
      ),
      2 => 
      array (
        'ID' => '1720',
        'post_title' => 'Git primer',
      ),
    )
    $new_array = array_column($array, 'post_title', 'ID');
    print_r($new_array);
    

    Output:

    Array
    (
        [1] => Hello world!
        [79] => ffffffffffff
        [1720] => Git primer
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型