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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化