PHP将数组按两个字段排序为子数组

时间:2014-07-03 20:50:28

标签: php arrays

我有以下数组:

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
    )
  )
 )

我需要做的是将此数组排序为子数组,其中start_date和end_date都匹配如下:

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
          )
       )
   )
)

我知道如何根据一个字段做到这一点,但我还没有完全管理两个字段。

修改: 我基本上试图做的不是那么多,而是更像是数组的元素到子阵列中。

1 个答案:

答案 0 :(得分:2)

或者,你可以创建一个新的。当然你需要循环它们并根据start_dateend_date对它们进行分组(我不知道Cake是否有一个优雅的解决方案来执行此任务,我还没有使用它。)。考虑这个例子:

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

应该产生这个群体:

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
            )
        )
    )
)