Skip to content

Instantly share code, notes, and snippets.

@emtudo
Created September 3, 2016 17:51
Show Gist options
  • Save emtudo/50f38171f880cba380423da32ccbf7eb to your computer and use it in GitHub Desktop.
Save emtudo/50f38171f880cba380423da32ccbf7eb to your computer and use it in GitHub Desktop.
if (!function_exists('array_rows_to_columns')) {
/**
* rows to columns.
*
* @param array $array
* @return array
*/
function array_rows_to_columns(array $data)
{
$newArray = [];
$lastKey = null;
$lastValue = null;
$oldArray = $data;
if (count($data) == 1) {
foreach ($data as $key => $value) {
if (is_array($value)) {
return [$key => $value];
}
return $value;
}
}
foreach ($data as $key => $value) {
if (is_null($lastKey)) {
$lastKey = $key;
$lastValue = $value;
continue;
}
if (isset($oldArray[$lastKey])) {
unset($oldArray[$lastKey]);
$newArray[$lastValue] = array_rows_to_columns($oldArray);
}
}
return $newArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment