Created
July 19, 2018 12:59
-
-
Save forsvunnet/9a0d8d89ee6ec60700bd2fa31e61a330 to your computer and use it in GitHub Desktop.
Dot path resolver
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
<?php | |
function convert(&$arr, $path, $value) { | |
$keys = explode('.', $path); | |
foreach ($keys as $key) { | |
if ( ! isset( $arr[$key] ) ) { | |
$arr[$key] = []; | |
} | |
$arr = &$arr[$key]; | |
} | |
$arr = $value; | |
} | |
$arr = [ | |
'entry', | |
'somethother'=>'something', | |
]; | |
convert( $arr, 'somethother.test.2', '11' ); | |
var_dump( $arr );% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment