Last active
December 19, 2019 04:05
-
-
Save adhocore/f98b4063af4982676b72 to your computer and use it in GitHub Desktop.
A helper function expand_array for PHP
This file contains 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
<?php | |
/** | |
* A helper function expand_array for PHP | |
* | |
* This is actually developed during the real need to develop JSON api | |
* I wanted to have JSON api response spitted properly expanded (with semantic nodes) | |
* But didnot want to do it by playing with arrays (looping, merging n' what not) time and again. | |
* So, After fetching single dimensional array data from database, with keys having | |
* some specific delimeter, this helper would do for me what i want to. | |
* PS: This thing here is designed to work in multidimensional (deep nested) array too. | |
* PPS: That example here is just simple teaser, what you can do &/or achieve is limitless. | |
* | |
* @author Jitendra Adhikari | adhocore <[email protected]> | |
* | |
* @param array $source The source array to expand | |
* @param string $delim The delimeter that is used to expand keys | |
* @param bool $deep Is the $source array deep nested ? (You better know it) | |
* | |
* @return array | |
*/ | |
function expand_array(array $source, $delim = '.', $deep = false) | |
{ | |
$deep and ksort($source); // Without this we would miss something | |
$target = array(); | |
// This closure is based on laravel's array helper; you can say i copied it ;) | |
$arraySet = function (&$array, $key, $value) use ($delim) { | |
if (is_null($key)) { | |
return $array = $value; | |
} | |
$keys = explode($delim, $key); | |
while (count($keys) > 1) { | |
$key = array_shift($keys); | |
if (! isset($array[$key]) or ! is_array($array[$key])) { | |
$array[$key] = []; | |
} | |
$array = & $array[$key]; | |
} | |
$array[array_shift($keys)] = $value; | |
return $array; | |
}; | |
foreach ($source as $key => $value) { | |
if (is_array($value)) { | |
$value = expand_array($value, $delim, $deep); | |
} | |
if (strpos($key, $delim) !== false) { | |
$arraySet($target, $key, $value); | |
} else { | |
$target[$key] = $value; | |
} | |
} | |
return $target; | |
} | |
// Usage: | |
// Suppose this is what i fetch from database | |
$source = array( | |
array( | |
'id' => 1, | |
'username' => 'adhocore', | |
'name.first' => 'Jitendra', | |
'name.last' => 'Adhikari', | |
'address.city' => 'Ktm', | |
'address.state' => 'Bagmati', | |
), | |
array( | |
'id' => 2, | |
'username' => '_adhocore', | |
'name.first' => '_Jitendra', | |
'name.last' => '_Adhikari', | |
'address.city' => '_Ktm', | |
'address.state' => '_Bagmati', | |
), | |
); | |
// Expand it, Yay ! | |
$target = expand_array($source, '.'); | |
print_r($target); | |
// Whoa, This is what i want | |
echo json_encode($target, JSON_PRETTY_PRINT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment