dongxixia6399 2019-03-21 20:56
浏览 42

PHP转置关联数组并填充丢失的单元格?

I have the following data which has gender and age category specific results of a race. The data is returned as an associative array.

In most categories I have three results, but in certain categories there might only be 1 or 2 results.

M50,1,9197,V50,M
M50,2,8253,V50,M
M50,3,18282,V50,M
W50,1,19961,V50,W
W50,2,7051,V50,W
W50,3,2480,V50,W
M55,1,7876,V55,M
M55,2,7640,V55,M
M55,3,19945,V55,M
W55,1,9029,V55,W
W55,2,6161,V55,W
W55,3,1172,V55,W
M60,1,7768,V60,M
M60,2,7112,V60,M
M60,3,7924,V60,M
W60,1,8747,V60,W
M65,1,7211,V65,M
M65,2,5234,V65,M
W65,1,8732,V65,W
M70,1,2952,V70,M
M70,1,2953,V70,W
M80,1,7953,V80,W

I want to transpose the array, so that each category is displayed as a single row, with a max of 6 results per row but any missing rows are filled with a blank.

M50 M1, M50 M2, M50 M3, M50 W1, M50 W2, M50 M3
M55 M1, M55 M2, M55 M3, M55 W1, M55 W2, M55 M3
M60 M1, M60 M2, M60 M3, M60 W1, x     , x
M65 M1, M65 M2, x     , M65 W1, x     , x
M70 M1, x     , x     , M70 W1, x     , x
x     , x     , x     , M80 W1, x     , x

Is there any recommendation on how i can achieve this in an efficent manner?

  • 写回答

2条回答 默认 最新

  • dongluolie3487 2019-03-21 22:04
    关注

    This might be a solution to what you are looking for:

    <?php
    
    $data = array(
        array('M50',1,9197,'V50','M'),
        array('M50',2,8253,'V50','M'),
        array('M50',3,18282,'V50','M'),
        array('W50',1,19961,'V50','W'),
        array('W50',2,7051,'V50','W'),
        array('W50',3,2480,'V50','W'),
        array('M55',1,7876,'V55','M'),
        array('M55',2,7640,'V55','M'),
        array('M55',3,19945,'V55','M'),
        array('W55',1,9029,'V55','W'),
        array('W55',2,6161,'V55','W'),
        array('W55',3,1172,'V55','W'),
        array('M60',1,7768,'V60','M'),
        array('M60',2,7112,'V60','M'),
        array('M60',3,7924,'V60','M'),
        array('W60',1,8747,'V60','W'),
        array('M65',1,7211,'V65','M'),
        array('M65',2,5234,'V65','M'),
        array('W65',1,8732,'V65','W'),
        array('M70',1,2952,'V70','M'),
        array('M70',1,2953,'V70','W'),
        array('M80',1,7953,'V80','W')
    );
    
    function extractData($data)
    {
        $result = array();
        $resultArrayTemplate = array('x', 'x', 'x', 'x', 'x', 'x');
        foreach($data as $item)
        {
            $value = $item[0] . ' ' . $item[4] . $item[1];
            $index = filter_var($item[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
            if(isset($result[$index])) {
                if($result[$index][$item[1]-1] != x) {
                    $subindex = $item[1]-1+3;
                } else {
                    $subindex = $item[1]-1;
                }
                $result[$index][$subindex] = $value;
            } else {
                if($result[$index][$item[1]-1] != x) {
                    $subindex = $item[1]-1+3;
                } else {
                    $subindex = $item[1]-1;
                }
                $result[$index] = $resultArrayTemplate;
                $result[$index][$subindex] = $value;
            }
        }
    
        return $result;
    }
    
    print_r(extractData($data));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算