can’t figure out how to insert a new string in a specific emplacement in an array regarding a given uri.
to be more specific,
I’m trying to create a function like the following one :
function insertNewFolder($name, $path){
$struct = json_decode( file_get_contents('structure.json'), true );
//at this place I want to include $name in the array according the given $path
}
let’s assume $struct is the following array :
$struct = array( 'folder' => array('subfolder1', 'subfolder2') );
NOTE: the scope can be more than one subfolder.
example : array( 'folder' => array( 'subfolder' => array( 'subsubfolder' )));
If I call the method like this insertNewFolder('subfolder3', 'folder'); the resulting array should be :
$struct = array( 'folder' => array('subfolder1', 'subfolder2', 'subfolder3') );
this is my try :
function insertNewFolder($name, $path){
...
// insert the new folder according the path
$segments = explode('/', $path);
$arr_path = '';
foreach( $segments as $s ){
$arr_path .= "['$s']";
}
eval("/$menu$arr_path[] = '$name';");
...
}
This would work if the eval method weren’t throwing an exception :
Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 16
I beg your help, is there a better way to reach this end or how to round this exception ?
Regards
You can do something similar to this (demo):