在php中将一个数组分成两个不同的数组

时间:2014-11-25 05:41:12

标签: php arrays codeigniter codeigniter-2

我的问题是我想根据开始时间将数组分成两个不同的数组,我的数组看起来像这样

Array
(
    [0] => Array
        (
            [day_of_weeks] => Monday
            [start_time] => 6:00 PM
            [end_time] => 7:00 PM
        )

    [1] => Array
        (
            [day_of_weeks] => Tuesday
            [start_time] => 6:00 PM
            [end_time] => 7:00 PM
        )

    [2] => Array
        (
            [day_of_weeks] => Wednesday
            [start_time] => 6:00 PM
            [end_time] => 7:00 PM
        )

    [3] => Array
        (
            [day_of_weeks] => Friday
            [start_time] => 5:00 PM
            [end_time] => 6:00 PM
        )

    [4] => Array
        (
            [day_of_weeks] => Saturday
            [start_time] => 5:00 PM
            [end_time] => 6:00 PM
        )

    [5] => Array
        (
            [day_of_weeks] => Sunday
            [start_time] => 5:00 PM
            [end_time] => 6:00 PM
        )

)

类似这样的事情

Array(
    [0] => Array
    (
        [day_of_weeks] => Monday
        [day_of_weeks] => Tuesday
        [day_of_weeks] => Wednesday
        [start_time] => 6:00 PM
        [end_time] => 7:00 PM
    )
    [1] => Array
    (
        [day_of_weeks] => Friday
        [day_of_weeks] => Saturday
        [day_of_weeks] => Sunday
        [start_time] => 5:00 PM
        [end_time] => 6:00 PM
    )
)

并且数据动态地来自数据库。请帮助我

2 个答案:

答案 0 :(得分:1)

这不是一个完整的解决方案,但可能会让您了解如何继续:

$array = array(
  array(
    'day_of_weeks' => 'Monday',
    'start_time' => '6:00 PM',
    'end_time' => '7:00 PM'
  ),
  array(
    'day_of_weeks' => 'Tuesday',
    'start_time' => '6:00 PM',
    'end_time' => '7:00 PM'
  ),
  array(
    'day_of_weeks' => 'Friday',
    'start_time' => '5:00 PM',
    'end_time' => '6:00 PM'
  )
);
$result = array();

// walk over each element of the original array
array_walk($array, function(&$item, $key) use (&$result) {
   // if the key denoted by `start_time` exists append the `day_of_weeks`
   if(array_key_exists($item['start_time'], $result)) {
      $result[$item['start_time']]['day_of_weeks'][] = $item['day_of_weeks'];
   } else {
      // make `day_of_weeks` an array that can hold more values
      $result[$item['start_time']] = array(
        'start_time' => $item['start_time'],
        'end_time' => $item['end_time'],
        'day_of_weeks' => array($item['day_of_weeks'])
      );
   } 
});

// the output of $result would be
Array
(
  [6:00 PM] => Array
  (
        [start_time] => 6:00 PM
        [end_time] => 7:00 PM
        [day_of_weeks] => Array
        (
           [0] => Monday
           [1] => Tuesday
        )
  )
  [5:00 PM] => Array
  (
        [start_time] => 5:00 PM
        [end_time] => 6:00 PM
        [day_of_weeks] => Array
        (
           [0] => Friday
        )
  )
)

也许您应该能够更改mysql查询以获得所需的输出。例如,针对下表运行此查询:

SELECT GROUP_CONCAT(day_of_weeks) day_of_weeks, 
       start_time, 
       end_time 
FROM data 
GROUP BY start_time 
ORDER BY start_time DESC;

+--------------+------------+----------+
| day_of_weeks | start_time | end_time |
+--------------+------------+----------+
| Monday       | 6:00 PM    | 7:00 PM  |
| Tuesday      | 6:00 PM    | 7:00 PM  |
| Wednesday    | 6:00 PM    | 7:00 PM  |
| Friday       | 5:00 PM    | 6:00 PM  |
| Saturday     | 5:00 PM    | 6:00 PM  |
| Sunday       | 5:00 PM    | 6:00 PM  |
+--------------+------------+----------+

以上示例将返回属于由start_time字符分隔的同一,的所有日期。请注意,end_time的值并未真正使用,对于上一个使用array_walk()的示例也是如此。

+--------------------------+------------+----------+
| day_of_weeks             | start_time | end_time |
+--------------------------+------------+----------+
| Monday,Tuesday,Wednesday | 6:00 PM    | 7:00 PM  |
| Friday,Saturday,Sunday   | 5:00 PM    | 6:00 PM  |
+--------------------------+------------+----------+

然后,您可以使用explode()或类似内容获取每一天:

foreach($results as $record) {
  $days = explode(",", $record['day_of_weeks'];
}

答案 1 :(得分:1)

首先,您不能为day_of_weeks添加多个不同的值,因为您已在所需的输出中写入,但是您可以使day_of_weeks成为一个包含日期名称的数组。

此外,由于您似乎使用start_timeend_time作为所需输出中的唯一键,我建议您在数组键中使用它们来简化程序。

这样就给了我们:

$output_array = array();
foreach ($input_array as $data_item) {
    $time_key = $data_item['start_time'] . ' - ' . $data_item['end_time'];
    if (isset($output_array[$time_key]['day_of_weeks'])) {
        $output_array[$time_key]['day_of_weeks'][] = $data_item['day_of_weeks'];
    } else {
        $output_array[$time_key] = array(
            'day_of_weeks' => array($data_item['day_of_weeks']),
            'start_time'   => $data_item['start_time'],
            'end_time'     => $data_item['end_time'],
        );
    }
}

这将导致:

Array(
    ['6:00 PM - 7:00 PM'] => Array
    (
        [day_of_weeks] => Array(
            [0] => Monday,
            [1] => Tuesday,
            [2] => Wednesday,
        ),
        [start_time] => 6:00 PM
        [end_time] => 7:00 PM
    )
    ['5:00 PM - 6:00 PM'] => Array
    (
        [day_of_weeks] => Array(
            [0] => Friday,
            [1] => Saturday,
            [2] => Sunday,
        ),
        [start_time] => 5:00 PM
        [end_time] => 6:00 PM
    )
)