Skip to content

Instantly share code, notes, and snippets.

@wujku
Created January 27, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wujku/3e99d29839963dc0ba12 to your computer and use it in GitHub Desktop.
Save wujku/3e99d29839963dc0ba12 to your computer and use it in GitHub Desktop.
<?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