在关于 php 中 uri 的数组中添加一个字符串
无法弄清楚如何在有关给定 uri 的数组中的特定位置插入新字符串。
更具体地说,
我正在尝试创建一个如下所示的函数:
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
}
假设 $struct 是以下数组:
$struct = array( 'folder' => array('subfolder1', 'subfolder2') );
注意:范围可以是多个子文件夹。 示例: array( 'folder' => array( 'subfolder' => array( 'subsubfolder' )));
如果我调用这样的方法 insertNewFolder('subfolder3', 'folder');
结果数组应该是:
$struct = array( 'folder' => array('subfolder1', 'subfolder2', 'subfolder3') );
这是我的尝试:
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';");
...
}
如果 eval 方法没有抛出异常,这将起作用:
Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 16
我请求您的帮助,是否有更好的方法来达到此目的或如何解决此异常?
问候
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行类似于此的操作(演示):
You can do something similar to this (demo):
我不太确定你想要什么,但为了得到你想要的结果为什么不使用:
除此之外,尽量避免 eval()。 99.99% 你可以不用......
如果你有一个多维数组,比如:
I'm not quite sure what you want but to get the result what you want why not use:
Besides that, try to avoid eval(). In 99.99% you can do without...
If you have a multidimensional array so something like:
您的复合数据结构未标准化,这使得很难解决您的问题。
请参阅您给出的第一个示例:
文件夹是文件夹的数组键,但子文件夹名称是数组值。
您的第二个示例使这一点更加清楚:
您现在遇到了使用插入操作处理每种可能情况的问题,但是您没有在问题中写下如何处理不同的情况,例如您想要在哪里插入什么以及如何插入
path
映射到数组键或值。您应该考虑重新表述您的问题,添加缺少的信息并使用更简单的语言和更好的示例。如果有帮助的话画个图吧。
Your composite data structure is not normalized, which makes it hard to solve your problem.
See the first example you gave:
The folder is an array key for
folder
, but the subfolder names are array values.Your second example makes this even more clear:
You now have the problem to deal with each probable case with your insert operation, but you have not written in your question how to deal with the different cases, e.g. where you want to insert what and how
path
maps to array keys or values.You should consider to rephrase your question, add the missing information and use more simple language and better examples. Draw a picture if it helps.