Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
6  
Well, what have you tried? Please post previous/current attempts and where exactly they fail you –  kingkero Jul 3 at 20:52
    
you need to create your own function for that. And use usort method –  Ronak Patel Jul 3 at 20:53
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? –  user3622217 Jul 3 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. –  user3622217 Jul 3 at 20:58
2  
Because this is the array returned to me by my mysql call from the CakePHP Framework. –  user3622217 Jul 3 at 21:01

1 Answer 1

up vote 2 down vote accepted

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
            )
        )
    )
) 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.