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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?