Created
September 3, 2016 17:51
-
-
Save emtudo/50f38171f880cba380423da32ccbf7eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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