I have a multidimensional array with 3 keys (\"length\", \"width\" and \"height). Each key is associated with an array of values:
$data = [
\"length\" =>
The following function will do the job:
function transpose($data)
{
$result = [];
$keys = array_keys($data);
for ($row = 0, $rows = count(reset($data)); $row < $rows; $row++) {
foreach ($keys as $key) {
$result[$row][$key] = $data[$key][$row];
}
}
return $result;
}
Notice that the function is a general solution it doesn’t depend on the name of the keys nor on the number of entries of each key.