2017-10-16 127 views
0

我想按日期對此數組進行重新排列/組合,並將數值合併到同一日期。PHP按值重新排列數組和組

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;   
} 
+0

你嘗試過這麼遠嗎? –

+1

如果您使用的是laravel,爲什麼不在您的查詢中使用group by和sort? – Miggy

回答

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 
     ) 

) 
+0

謝謝@Taha Paksu。問題解決了。 :) – darkvirus24

+0

不客氣。 –

0

可以使用usort功能

$array = usort($array, function($a, $b){ 
    return date('Y-m-d', strtotime($a['date'])) > date('Y-m-d', 
     strtotime($b['date'])) 
}) 
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);