2

This sounds quite simple, but i can't make it work. I'm trying to group keys with same value. I can get the key number but i cant get the name of the Key. i.e "London, Berlin". This is my code:

$countries = array (
   'London' => 'Europe/London',
   'Istanbul' => 'Europe/Istanbul',
   'Rome' => 'Europe/Rome',
   'Berlin' => 'Europe/Berlin',
   'Athens' => 'Europe/Athens',
);


$offsets = Array();
foreach ($countries as $country_offset) {
   $offset = timezone_offset_get( new DateTimeZone( $country_offset ), new DateTime() );

   array_push($offsets, $offset);
}

$result = array_unique($offsets);
asort($result);

$keys = array_keys($result);
foreach($keys as $key) {
   $numb = array_keys($offsets, $offsets[$key]);

   echo $offsets[$key] . ' - ' . implode(', ', $numb ) . '<br>';
}
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
Avel
  • 111
  • 1
  • 2
  • 9
  • i dont understand what the desired output of this should be –  Mar 30 '16 at 02:06
  • Maybe you are looking for this: http://php.net/manual/en/function.array-search.php – Ikari Mar 30 '16 at 02:08
  • @Dagon The name of the Key `'London' => 'Europe/London'`. The desired output `London`. – Avel Mar 30 '16 at 02:08
  • you can't map the keys anymore from the output, you've transformed it into another form, just create another container which contains all pertinent data so that you can have all info – Kevin Mar 30 '16 at 02:09
  • The hole output is something like this `7200 - 2, 3`. What i'm trying to do is to have names instead of index number i.e. `7200 - Rome, Berlin` – Avel Mar 30 '16 at 02:14

2 Answers2

2

I'd suggest just create the full info array grouping first that includes the key that you want, instead of creating the presentation mapping the key of the original input.

Idea:

$offsets = array(); // initialize
foreach($countries as $key => $country_offset) { // grouping
    $offset = timezone_offset_get( new DateTimeZone( $country_offset ), new DateTime() );
    $offsets[$offset][] = array(
        'name'      => $key, // include me instead!
        'offset'    => $offset,
        'timezome'  => $country_offset,
    );
}
ksort($offsets); // sort

The important bit here is that, group them inside a container using the offset as your key:

$offsets[$offset][] = array(
//       ^ reassignment grouping using the offset as key

Then, in your presentation, decide what you want:

// presentation
foreach($offsets as $offset => $info) {
    echo $offset . ' - ';
    $temp = array();
    foreach($info as $t) {
        $temp[] = $t['name'];
    }
    echo implode(', ', $temp);
    echo '<br/>';
}

If array_column is available, just use it:

foreach($offsets as $offset => $info) {
    echo $offset . ' - ' . implode(', ', array_column($info, 'name')) . '<br/>';
}

Sample Output

Kevin
  • 41,694
  • 12
  • 53
  • 70
1
 <?php
$countries = array (
   'London' => 'Europe/London',
   'Istanbul' => 'Europe/Istanbul',
   'Rome' => 'Europe/Rome',
   'Berlin' => 'Europe/Berlin',
   'Athens' => 'Europe/Athens',
);

$out=array();
foreach ($countries as $country_offset=>$c) {
   $offset = timezone_offset_get( new DateTimeZone( $c ), new DateTime() );


$out[$offset][]=$country_offset;
}
//print_r($out);

foreach($out as $x=>$y){

echo $x.': '.implode(',',$y).'<br>';
}

//output:

3600: London
10800: Istanbul,Athens
7200: Rome,Berlin