<?php
$myArray = array(
'Europe' => 'World',
'Asia' => 'World',
'Africa' => 'World',
'France' => 'Europe',
'Poland' => 'Europe',
'Germany' => 'Europe',
'Paris' => 'France',
'Lille' => 'France',
'Warsaw' => 'Poland',
'Szczecin' => 'Poland',
'Berlin' => 'Germany',
'Gumience' => 'Szczecin'
);
foreach($myArray as $key => $val)
{
$element = build($key, $myArray);
if ($element)
{
$result[$element][$key] = $value;
}
else
{
$result[][$value] = array();
}
}
function build($needle, $haystack)
{
foreach($haystack as $key => $value)
{
if ($needle === $key OR (is_array($value) && build($needle, $value) !== false))
{
return $value;
}
}
return false;
}
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
print_r2($myArray);
echo ('<br /><br />');
print_r2($result);
?>
preferences:
31.67 ms | 404 KiB | 5 Q