在关于 php 中 uri 的数组中添加一个字符串

发布于 2024-12-09 16:59:29 字数 1277 浏览 2 评论 0原文

无法弄清楚如何在有关给定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你手 2024-12-16 16:59:29

您可以执行类似于的操作(演示):

function insertValueByPath($array, $path, $value) {
    $current = &$array;
    foreach (explode('/', $path) as $part) {
        $current = &$current[$part];
    }
    $current = $value;

    return $array;
}

$struct = array(
    'folder' => array(
        'subfolder1' => array(),
        'subfolder2' => array(),
    ),
);

$struct = insertValueByPath($struct, 'folder/subfolder1/subsubfolder', array());
$struct = insertValueByPath($struct, 'folder/subfolder2/subsubfolder/subsubsub/subsubsubsub', array('hallo'));

You can do something similar to this (demo):

function insertValueByPath($array, $path, $value) {
    $current = &$array;
    foreach (explode('/', $path) as $part) {
        $current = &$current[$part];
    }
    $current = $value;

    return $array;
}

$struct = array(
    'folder' => array(
        'subfolder1' => array(),
        'subfolder2' => array(),
    ),
);

$struct = insertValueByPath($struct, 'folder/subfolder1/subsubfolder', array());
$struct = insertValueByPath($struct, 'folder/subfolder2/subsubfolder/subsubsub/subsubsubsub', array('hallo'));
诗酒趁年少 2024-12-16 16:59:29

我不太确定你想要什么,但为了得到你想要的结果为什么不使用:

function insertNewFolder($name, $path){
   $struct[$name][]=$path;
}

除此之外,尽量避免 eval()。 99.99% 你可以不用......

如果你有一个多维数组,比如:

$segments = explode('/', $path);

  $arr_path = '';
  $tmp_struct=&$struct;
  for ($i=0;$i<(count($segments)-1);$i++) {
    if (!isset($tmp_struct[$segments[$i]]) {
      $tmp_struct[$segments[$i]]=1;
    }
      $tmp_struct=$tmp_struct[$segments[$i]];
  }
$tmp_struct[$segments[count($segments)-1]]=$name;

I'm not quite sure what you want but to get the result what you want why not use:

function insertNewFolder($name, $path){
   $struct[$name][]=$path;
}

Besides that, try to avoid eval(). In 99.99% you can do without...

If you have a multidimensional array so something like:

$segments = explode('/', $path);

  $arr_path = '';
  $tmp_struct=&$struct;
  for ($i=0;$i<(count($segments)-1);$i++) {
    if (!isset($tmp_struct[$segments[$i]]) {
      $tmp_struct[$segments[$i]]=1;
    }
      $tmp_struct=$tmp_struct[$segments[$i]];
  }
$tmp_struct[$segments[count($segments)-1]]=$name;
对风讲故事 2024-12-16 16:59:29

您的复合数据结构未标准化,这使得很难解决您的问题。

请参阅您给出的第一个示例:

array( 'folder' => array('subfolder1', 'subfolder2') );

文件夹是文件夹的数组键,但子文件夹名称是数组值。

您的第二个示例使这一点更加清楚:

array( 'folder' => array( 'subfolder' => array( 'subsubfolder' )));

您现在遇到了使用插入操作处理每种可能情况的问题,但是您没有在问题中写下如何处理不同的情况,例如您想要在哪里插入什么以及如何插入path 映射到数组键值。

您应该考虑重新表述您的问题,添加缺少的信息并使用更简单的语言和更好的示例。如果有帮助的话画个图吧。

Your composite data structure is not normalized, which makes it hard to solve your problem.

See the first example you gave:

array( 'folder' => array('subfolder1', 'subfolder2') );

The folder is an array key for folder, but the subfolder names are array values.

Your second example makes this even more clear:

array( 'folder' => array( 'subfolder' => array( 'subsubfolder' )));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文