0

I have this array where days are the key and timings are the values:

Array(

    [Mon] => 9:00-10:00,12:00-15:00,10:00-14:00

    [Tue] => 9:00-10:00,12:00-15:00

    [Wed] => 9:00-10:00,12:00-15:00

    [Thu] => 9:00-10:00,12:00-15:00

    [Fri] => 9:00-10:00,12:00-15:00

    [Sat] => 9:00-10:00,12:00-15:00

    [Sun] => 10:00-14:00

)

And I want same array in this way:

 Array(

    [Mon] => 9:00-10:00,12:00-15:00,10:00-14:00

    [Tue/Wed/Thu/Fri/Sat] => 9:00-10:00,12:00-15:00

    [Sun] => 10:00-14:00

)

any one can help me on this?

6
  • 1
    php.net/array_unique / php.net/array_keys / php.net/array_count_values - please take a look into the manual first. Also please make yourself comfortable with the editing tools on this website. The spaces in front are used for - code formatting. Commented Apr 5, 2013 at 11:58
  • 4
    No, you should first help yourself and try to code it. If your code does not work we will be very happy to help you.. Commented Apr 5, 2013 at 11:58
  • What is 9:00-10:00,12:00-15:00,10:00-14:00 in Mon? A string? An array? Commented Apr 5, 2013 at 12:07
  • @Pee - unable to save the same keys in one key for a same value Commented Apr 5, 2013 at 12:08
  • @ihsan - i have tried from my end, am not getting the result, so i posted here dear Commented Apr 5, 2013 at 12:09

5 Answers 5

1

There may be a better way to achieve this, but the following should work:-

$input = <YOUR ARRAY>
$output = array();

foreach ($input as $k => $v) {
  if (!isset($output[$v])) {
    $output[$v] = $k;
  } else {
    $output[$v] .= '/' . $k;
  }
}

print_r(array_flip($output));

Here's a working example

Sign up to request clarification or add additional context in comments.

Comments

0
$temp = array();
foreach ($array as $day => $value) {
    if (!in_array($value, $temp)) {
        $temp[] = $value;
        $result[implode("/",array_keys($array,$value))] = $value;
    }
}

See DEMO.

Comments

0
$data = array(
    'Mon' => '9:00-10:00,12:00-15:00,10:00-14:00',
    'Tue' => '9:00-10:00,12:00-15:00',
    'Wed' => '9:00-10:00,12:00-15:00',
    'Thu' => '9:00-10:00,12:00-15:00',
    'Fri' => '9:00-10:00,12:00-15:00',
    'Sat' => '9:00-10:00,12:00-15:00',
    'Sun' => '10:00-14:00'
    );

$new_data = array();

foreach($data as $key => $value){
    $index = array_search($value, $new_data);
    if($index){
        $new_data[$index .'/' . $key] = $value;
        unset($new_data[$index]);
    }else{
        $new_data[$key] = $value;
    }
}

echo '<pre>';
var_dump($new_data);
echo '</pre>';

Comments

0

So, there my long example.

$date_array = Array(
    'Mon' => '9:00-10:00, 12:00-15:00, 10:00-14:00',
    'Tue' => '9:00-10:00, 12:00-15:00',
    'Wed' => '9:00-10:00, 12:00-15:00',
    'Thu' => '9:00-10:00, 12:00-15:00',
    'Fri' => '9:00-10:00, 12:00-15:00',
    'Sat' => '9:00-10:00, 12:00-15:00',
    'Sun' => '10:00-14:00',
);

$previous = null;
$new_days = array();
$new_times = array();
$i = 0;
foreach ($date_array as $day => $time) {
    if ($time === $previous) {
        $new_days[$i] .= '/' . $day;
        $new_times[$i] = $time;
    } else {
        $i++;
        $new_days[$i] = $day;
        $new_times[$i] = $time;
    }
    $previous = $time;
}

var_dump(array_combine($new_days, $new_times));

Comments

0

After re-viewing your question I came to the following solution, basically using foreach and using the value to aggregate days for as the key collecting the days (first foreach); then imploding the days into a single string (second foreach) and then flipping the array:

$output = array();

foreach ($input as $day => $time) $output[$time][] = $day;

foreach ($output as &$value) $value = implode('/', $value); 
unset ($value);

print_r(array_flip($output));

http://eval.in/14944

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.