2013-06-20 85 views
2

對象的數組我想通過「日期」字段排序的以下選項卡並計算「臨時工」字段的總和:排序字段

0 =>  array (size=3)  'id_cra' => string '4586' (length=4)  'temps' => string '03:00:00' (length=8)  'date' => string '2013-06-03' (length=10) 
1 =>  array (size=3)  'id_cra' => string '4587' (length=4)  'temps' => string '03:00:00' (length=8)  'date' => string '2013-06-06' (length=10) 
2 =>  array (size=3)  'id_cra' => string '4588' (length=4)  'temps' => string '03:00:00' (length=8)  'date' => string '2013-06-05' (length=10) 
3 =>  array (size=3)  'id_cra' => string '4589' (length=4)  'temps' => string '03:00:00' (length=8)  'date' => string '2013-06-06' (length=10) 
4 =>  array (size=3)  'id_cra' => string '4590' (length=4)  'temps' => string '03:00:00' (length=8)  'date' => string '2013-06-07' (length=10) 
5 =>  array (size=3)  'id_cra' => string '4591' (length=4)  'temps' => string '02:00:00' (length=8)  'date' => string '2013-06-03' (length=10) 
6 =>  array (size=3)  'id_cra' => string '4592' (length=4)  'temps' => string '03:30:00' (length=8)  'date' => string '2013-06-03' (length=10) 

預期的結果:

在 「臨時工」 字段日期=>點心

2013-06-03 =>08:30:00 
2013-06-06 =>06:00:00 

PS:對不起,問的東西,可能對你很簡單,我已經在互聯網上檢查過(例如:Sort array of objects by object fields),但我不明白答案

+0

所以,你想先按日期分組,然後按日期排序? –

+1

如果你結合日期,你會失去'id_cra'數據 –

+0

@Samuel Cook:沒關係! – BYU

回答

1

第一部分非常簡單。我們可以爲一個數組中的數據通過使用array_multisort()場像這樣排序:

<?php 
$data = array(
    0 => array('id_cra' => '4586', 'temps' => '03:00:00', 'date' => '2013-06-03'), 
    1 => array('id_cra' => '4587', 'temps' => '03:00:00', 'date' => '2013-06-06'), 
    2 => array('id_cra' => '4588', 'temps' => '03:00:00', 'date' => '2013-06-05'), 
    3 => array('id_cra' => '4589', 'temps' => '03:00:00', 'date' => '2013-06-06'), 
    4 => array('id_cra' => '4590', 'temps' => '03:00:00', 'date' => '2013-06-07'), 
    5 => array('id_cra' => '4591', 'temps' => '02:00:00', 'date' => '2013-06-03'), 
    6 => array('id_cra' => '4592', 'temps' => '03:30:00', 'date' => '2013-06-03') 
); 

$tmp = array(); 
foreach($data as $k=>$r){ 
    $tmp[] = $r['date']; 
} 
array_multisort($tmp,SORT_DESC,$data); 

echo '<pre>',print_r($data),'</pre>'; 

但現在要添加次,每次的日子。這並不困難,但是您將失去id_cra值。所以對於下面的例子,我剛剛創建了一個新的數組。如果你想要做什麼,你應該能夠使用這些信息將其重新構建到原始數組中。

date_default_timezone_set('America/New_York'); //set timezone 
$midnight = mktime(0,0,0,date("n"),date("j"),date("Y")); // create a constant 
$times = array(); 
foreach($data as $d){ 
    // get number of secs since const 
    $time = strtotime(date("Y-m-d",$midnight).' '.$d['temps']) - $midnight; 

    if(isset($times[$d['date']])){ 
     $times[$d['date']] += $time; // if day exists add secs 
    }else{ 
     $times[$d['date']] = $time; // else set day with cur secs 
    } 
} 

foreach($times as $k=>&$time){ 
    $time = date("Y-m-d H:i:s",strtotime($k)+$time); // reformat to date 
} 

echo '<pre>',print_r($times),'</pre>'; 
+0

對不起,您訪問了一個老問題,但是當您的代碼正確時,您的示例會給出一個數組列表,而原始問題會提供一個對象列表。因此array_multisort()可能不是這裏需要的解決方案。 – crafter

2

我沒有嘗試,但它必須是這樣的:

$arr = [your array data]; // your array which has to be sorted 

usort($arr, 'sort_array'); 

function sort_array($a, $b) { 

    $timeA = strtotime($a['date']); 
    $timeB = strtotime($b['date']); 

    if($timeA == $timeB) { 
     return 0; 
    } 
    return $timeA < $timeB ? -1 : 1; 
} 

計算的金額排序,你必須「組」按日期排列後,將是一個循環,總結「臨時工」