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

时间:2017-10-16 09:19:31

标签: javascript php mysql arrays laravel

我想按日期重新排列/分组此数组,并将值合并为相同的日期。

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 
        )

)

输出将是:

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
        )
)

我曾尝试过:

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

3 个答案:

答案 0 :(得分:1)

在这里准备一个小提琴:

https://eval.in/881325

这里是完整的代码:

<?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);
?>

输出:

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
        )

)

答案 1 :(得分:0)

您可以使用usort功能

$array = usort($array, function($a, $b){
  return date('Y-m-d', strtotime($a['date'])) > date('Y-m-d', 
         strtotime($b['date']))
})

答案 2 :(得分:0)

试试这个:

$auxResult = array();
foreach ($query as $item) {
    if (!isset($auxResult[$item->date])) {
        $auxResult[$item->date] = array();
    }

    $auxResult[$item->date] = (object) array_merge((array) $auxResult[$item->date], (array) $item);
}

$result = array_values($auxResult);