1

I have the following array:

array(
 [0] =>array(
   'Users'=>array(
        'id'=>2,
        'start_date'=>2014-02-05,
        'end_date'=>2014-02-09
    )
  ),
  [1]=> array(
    'Users'=>array(
        'id'=>3,
        'start_date'=>2014-02-05,
        'end_date'=>2014-02-09
    )
  ),
  [2]=> array(
    'Users'=>array(
        'id'=>4,
        'start_date'=>2014-02-09,
        'end_date'=>2014-02-12
    )
  ),
  [3]=> array(
    'Users'=>array(
        'id'=>5,
        'start_date'=>2014-02-15,
        'end_date'=>2014-02-25
    )
  )
 )

What I need to do is sort this array into subarrays where both start_date and end_date match like this:

array(
   [0] => array(
      'Users'=>array(
          [0] => array(
            'id'=>2,
            'start_date'=>2014-02-05,
            'end_date'=>2014-02-09
          ),
          [1] => array(
            'id'=>3,
            'start_date'=>2014-02-05,
            'end_date'=>2014-02-09
          )
       )
   ),
   [1] => array(
      'Users'=>array(
          [0] => array(
            'id'=>4,
            'start_date'=>2014-02-09,
            'end_date'=>2014-02-12
          )
       )
   ),
   [2] => array(
      'Users'=>array(
          [0] => array(
            'id'=>5,
            'start_date'=>2014-02-15,
            'end_date'=>2014-02-25
          )
       )
   )
)

I know how I can do it based on one field, but I haven't quite managed two.

EDIT: What I am basically trying to do is not so much a sort but more of group like elements of an array into subarrays.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
chrisShick
  • 1,096
  • 8
  • 21
  • 6
    Well, what have you tried? Please post previous/current attempts and where exactly they fail you – kero Jul 03 '14 at 20:52
  • you need to create your own function for that. And use usort method – RNK Jul 03 '14 at 20:53
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – RNK Jul 03 '14 at 20:55
  • 1
    I don't really know where to start with it when it has to sort into subarrays by two fields. I was thinking to sort the array by dates and then to check where the dates change? – chrisShick Jul 03 '14 at 20:55
  • 1
    What I am basically trying to do is not so much a sort but more of group like elements of an array into subarrays. – chrisShick Jul 03 '14 at 20:58
  • Then why even bother with sort and not just create a new array in the form you want? – kero Jul 03 '14 at 21:00
  • 2
    Because this is the array returned to me by my mysql call from the CakePHP Framework. – chrisShick Jul 03 '14 at 21:01
  • I'd just loop over the array a save a copy of previous_dates (from previous iteration of the loop) and current_dates. When these are not equal, you've hit the boundary of the date range. Run code in this case, then update previous_date. I'm assuming it's already sorted by date. – Kevin Seifert Jul 04 '14 at 01:49

1 Answers1

2

Alternatively, you could just create a new one. Of course you need to loop them and group them according to start_date or end_date (I don't know if Cake has an elegant solution for this task, I haven't used it.). Consider this example:

$values = array(array('Users' => array(array('id' => 2, 'start_date' => '2014-02-05', 'end_date' => '2014-02-09'),)),array('Users' => array(array('id' => 3, 'start_date' => '2014-02-05', 'end_date' => '2014-02-09'),)),array('Users' => array(array('id' => 4, 'start_date' => '2014-02-09', 'end_date' => '2014-02-12'),)),array('Users' => array(array('id' => 5, 'start_date' => '2014-02-15', 'end_date' => '2014-02-25'),)),);
$new_values = array();
foreach($values as $key => $value) {
    $value = reset($value); // users
    foreach($value as $element) {
        // group them according to start and date date
        // make them an index
        $index = $element['start_date'] . ' to ' . $element['end_date'];
        $new_values[$index]['Users'][] = $element;
    }
}

$new_values = array_values($new_values); // simple reindexing, reset to numeric
echo '<pre>';
print_r($new_values);

Should yield this group:

Array
(
    [0] => Array
    (
        [Users] => Array
        (
            [0] => Array
            (
                [id] => 2
                [start_date] => 2014-02-05
                [end_date] => 2014-02-09
            )

            [1] => Array
            (
                [id] => 3
                [start_date] => 2014-02-05
                [end_date] => 2014-02-09
            )
        )
    )

    [1] => Array
    (
        [Users] => Array
        (
            [0] => Array
            (
                [id] => 4
                [start_date] => 2014-02-09
                [end_date] => 2014-02-12
            )
        )
    )

    [2] => Array
    (
        [Users] => Array
        (
            [0] => Array
            (
                [id] => 5
                [start_date] => 2014-02-15
                [end_date] => 2014-02-25
            )
        )
    )
) 
user1978142
  • 7,946
  • 3
  • 17
  • 20