Prova den här funktionen:
function array_transpose($array, $selectKey = false) {
if (!is_array($array)) return false;
$return = array();
foreach($array as $key => $value) {
if (!is_array($value)) return $array;
if ($selectKey) {
if (isset($value[$selectKey])) $return[] = $value[$selectKey];
} else {
foreach ($value as $key2 => $value2) {
$return[$key2][$key] = $value2;
}
}
}
return $return;
}
$fruits = array(
array('id' => 1, 'name' => 'Apple', 'color' => 'Red'),
array('id' => 2, 'name' => 'Orange', 'color' => 'Orange'),
array('id' => 3, 'name' => 'Mango', 'color' => 'Yellow')
);
echo "<pre>";
print_r(array_transpose($fruits));
echo "</pre>";
"; Returnerar:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => Apple
[1] => Orange
[2] => Mango
)
[color] => Array
(
[0] => Red
[1] => Orange
[2] => Yellow
)
)