Created
January 27, 2016 10:51
-
-
Save wujku/3e99d29839963dc0ba12 to your computer and use it in GitHub Desktop.
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 | |
/* | |
* Get value from array by path | |
* Example: | |
* $a = ['customer' => ['address' => 'St. Jork', 'city' => 'Miami']] | |
* | |
* getByPath($a, 'customer.address') | |
*/ | |
function getByPath($array, $path) { | |
$temp = &$array; | |
foreach (explode('.', $path) as $key) { | |
$temp =& $temp[$key]; | |
} | |
return $temp; | |
} | |
/* | |
* Set value into array by path | |
* | |
* setByPath($a, 'customer.postcode', '11334') | |
*/ | |
function setByPath(&$array, $path, $value) { | |
$temp = &$array; | |
foreach(explode('.', $path) as $key) { | |
$temp =& $temp[$key]; | |
} | |
$temp = $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment