du4629 2012-05-08 13:41
浏览 52
已采纳

将关联数组转换为索引数组的优雅方法

Having an associative array like

$myarray = array (
  'user' => array (
      2 => 'john',
      5 => 'done',
      21 => 'foo'
  ),
  'city' => array (
      2 => 'london',
      5 => 'buenos aires',
      21 => 'paris',
  ),
  'age' => array (
      2 => 24,
      5 => 38,
      21 => 16
  )
  ...
);

EDITED
Assuming I don't know how many keys this array has, nor the keys themselves (they could be whatever) The above is just an example

Is there any elegant way (using built-in functions and not loops) to convert it into

$result = array(
    array(
        'user',
        'city',
        'age'
        ...
    ),
    array(
        'john',
        'london',
        24
        ...
    ),
    array(
        'done',
        'buenos aires',
        38
        ...
    ),
    array(
        'foo',
        'paris',
        16
        ...
    )
);




As a side question: how to obtain this too (in a similar elegant way)

$result = array(
    array(
        'row',
        'user',
        'city',
        'age'
        ...
    ),
    array(
        2,
        'john',
        'london',
        24
        ...
    ),
    array(
        5,
        'done',
        'buenos aires',
        38
        ...
    ),
    array(
        21,
        'foo',
        'paris',
        16
        ...
    )
);
  • 写回答

2条回答 默认 最新

  • duanpasi6287 2012-05-08 13:44
    关注

    Note that both of these operations are basically the transpose of your original array.

    All of this is strongly adapted from this answer (consider upvoting it!).

    helper

    First we need a helper function, which keeps the row headers as the keys in the associative array:

    function flipArrayKeys($arr) {
        $out = array('row' => array_keys($arr));
        foreach ($arr as $key => $subarr) {
            foreach ($subarr as $subkey => $subvalue) {
                $out[$subkey][] = $subvalue;
            }
        }
        return $out;
    }
    

    flipArrayKeys($myarray) gives:

    array (
      'row' => 
      array (
        0 => 'user',
        1 => 'city',
        2 => 'age',
      ),
      2 => 
      array (
        0 => 'john',
        1 => 'london',
        2 => 24,
      ),
      5 => 
      array (
        0 => 'done',
        1 => 'buenos aires',
        2 => 38,
      ),
      21 => 
      array (
        0 => 'foo',
        1 => 'paris',
        2 => 16,
      ),
    )
    

    part 1

    $result = array_values(flipArrayKeys($myarray));
    

    and now result looks like:

    array (
      0 => 
      array (
        0 => 'user',
        1 => 'city',
        2 => 'age',
      ),
      1 => 
      array (
        0 => 'john',
        1 => 'london',
        2 => 24,
      ),
      2 => 
      array (
        0 => 'done',
        1 => 'buenos aires',
        2 => 38,
      ),
      3 => 
      array (
        0 => 'foo',
        1 => 'paris',
        2 => 16,
      ),
    )
    

    This part can also be done using transpose from this answer:

    $result = transpose($myarray);
    array_unshift($result, array_keys($myarray));
    

    part 2

    function flipArrayWithHeadings($arr) {
        $out = flipArrayKeys($arr);
    
        foreach (array_keys($out) as $key) {
            array_unshift($out[$key],$key);
        }
        return array_values($out);
    }
    

    So flipArrayWithHeadings($myarray) looks like:

    array (
      0 => 
      array (
        0 => 'row',
        1 => 'user',
        2 => 'city',
        3 => 'age',
      ),
      1 => 
      array (
        0 => 2,
        1 => 'john',
        2 => 'london',
        3 => 24,
      ),
      2 => 
      array (
        0 => 5,
        1 => 'done',
        2 => 'buenos aires',
        3 => 38,
      ),
      3 => 
      array (
        0 => 21,
        1 => 'foo',
        2 => 'paris',
        3 => 16,
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗