dta38159 2017-10-16 09:19
浏览 61
已采纳

PHP重新排列数组和按值分组

I want to re-arrange/ group this array by date, and merge values with the same date.

Array
(
    [0] => stdClass Object
        (
            [pc] => 100
            [date] => 2017-10-03 
        )

    [1] => stdClass Object
        (
            [pi] => 1
            [date] => 2017-10-16 
        )

    [2] => stdClass Object
        (
            [so] => 10
            [date] => 2017-10-12 
        )

    [3] => stdClass Object
        (
            [so] => 2
            [date] => 2017-10-16 
        )

)

Output will be:

Array
(
    [0] => stdClass Object
        (
            [date] => 2017-10-03 
            [pc] => 100
        )

    [1] => stdClass Object
        (
            [date] => 2017-10-12   
            [so] => 10
        )

    [2] => stdClass Object
        (
            [date] => 2017-10-16 
            [pi] => 1
            [so] => 2
        )
)

I had tried:

$new = array();
foreach ($query as $opt) {
    $date = date('Y-m-d',strtotime($opt->date));
    $new[$date] = $opt;         
}
  • 写回答

3条回答 默认 最新

  • dtgu21994537 2017-10-17 06:58
    关注

    Prepared a fiddle here:

    https://eval.in/881325

    Here's the complete code:

    <?php
    // initial array
    
    $array = [
        (object)[
            "pc" => 100,
            "date" => "2017-10-03"
        ],
        (object)[
            "pi" => 1,
            "date" => "2017-10-16"
        ],
        (object)[
            "so" => 10,
            "date" => "2017-10-12"
        ],
        (object)[
            "so" => 2,
            "date" => "2017-10-16"
        ]
    ];
    
    // formatted array container
    
    $newArray = [];
    
    // loop each variable and collect the same dated values 
    // together using date as the key
    
    foreach($array as $val){
        if(!isset($newArray[$val->date])) $newArray[$val->date] = [];
        foreach(get_object_vars($val) as $key => $prop){
            if($key != "date"){
                $newArray[$val->date][$key] = $prop;
            }
        }
    }
    
    // restructure the array to convert the keys into sub keys
    $index = 0;
    foreach($newArray as $key => $value){
        $newArray[$index]["date"] = $key;
        foreach($value as $key2 => $value2){
            $newArray[$index][$key2] = $value2;
        }
        unset($newArray[$key]);
        $newArray[$index] = (object)$newArray[$index];
        $index++;
    }
    
    // sort the last array
    usort($newArray, function($a, $b){
        if(date_parse_from_format("YYYY-MM-DD", $a->date) > date_parse_from_format("YYYY-MM-DD", $b->date)){
            return 1;
        }else if(date_parse_from_format("YYYY-MM-DD", $a->date) < date_parse_from_format("YYYY-MM-DD", $b->date)){
            return -1;
        }else{
            return 0;
        }
    });
    
    // output the result
    print_r($newArray);
    ?>
    

    Outputs:

    Array
    (
        [0] => stdClass Object
            (
                [date] => 2017-10-03
                [pc] => 100
            )
    
        [1] => stdClass Object
            (
                [date] => 2017-10-12
                [so] => 10
            )
    
        [2] => stdClass Object
            (
                [date] => 2017-10-16
                [pi] => 1
                [so] => 2
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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