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.

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

1 Answer 1

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
            )
        )
    )
) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.