0

my question is i want to break array in to two different array based upon start time my array look like this

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
        )

)

something like this

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

and data is dynamically coming form database. Pls help me

4
  • what you have done so far Mohit? Commented Nov 25, 2014 at 5:42
  • @diEcho I tried so hard to do but at the end i did't get the result i tried and get this result Array ( [days] => Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [3] => Friday [4] => Saturday [5] => Sunday ) [start_time] => Array ( [0] => 11:00 AM [1] => 11:00 AM [2] => 11:00 AM [3] => 12:00 PM [4] => 12:00 PM [5] => 12:00 PM ) ) Commented Nov 25, 2014 at 5:46
  • 4
    Hi mohit, you cannot have a same key inside an array like what you need in output. as day_of_weeks Commented Nov 25, 2014 at 5:50
  • add the final query and the result in your question. It would be more helpful Commented Nov 25, 2014 at 5:50

2 Answers 2

1

This is not a complete solution but might give you some idea on how to proceed:

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

Perhaps you should be able to change your mysql query to get the desired output. For example running this query against the following table:

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

The above example would return all days belonging to the same start_time separated by a , character. Note that the value of end_time is not really used, this is also true for the previous example using 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  |
+--------------------------+------------+----------+

You could then get each day using explode() or similar:

foreach($results as $record) {
  $days = explode(",", $record['day_of_weeks'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

@MohitJain - Don't know if it will help, but I added a mysql solution that might do the job.
thanks again for sql query but your code is worked for me..no need to modify my sql query
@MohitJain - Just added it as an alternate solution.
1

First, you can't have multiple differing values for day_of_weeks as you have written in your desired output, however you can make day_of_weeks an array that holds the names of the days.

Also, since it appears you are using start_time and end_time as your unique key in your desired output, I would recommend using them in the array key itself to simply your program.

So that gives us:

$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'],
        );
    }
}

This will result in:

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

2 Comments

you solution give me last value of the array like in day_of_weeks Wednesday and Sunday
after modifying your code i almost got the result but above answer give me whole solution

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.