2

i would like to sort the following tab by the "date" field and calculate the sum of the "temps" field :

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)  

The expected result :

date = > Sum of the "Temps" fields

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

etc

PS: Sorry for asking something that might be simple for you, i already checked on internet (for instance here : Sort array of objects by object fields) but i don't understand the answer

Community
  • 1
  • 1
BYU
  • 75
  • 1
  • 13

2 Answers2

2

I didn't try, but it has to be something like this:

$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;
}

calculating the sums would be a loop after sorting the array where you have to "group" by date and sum the "temps"

leuchtdiode
  • 477
  • 3
  • 12
1

The first part is fairly simple. We can sort the data for an array by a field using array_multisort() like so:

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

But now want to add the times for each of the days. This isn't difficult, but you will lose the id_cra values. So for the example below I have just created a new array. You should be able to use the information to build it back into your original array if that what you want to do.

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>';
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • Sorry for visiting an old question, but while your code is correct, your example gives a list of arrays, while the original question asks about a list of objects. array_multisort() might therefore not be the solution required here. – crafter Mar 07 '18 at 15:47