有可能制作一个动态多维数组吗


Is it possible to make a dynamic multidimensional-array?

是否可以创建动态多维数组?

我想用数组做一个递归函数。所以,如果我能说一些类似的话,那就太好了

$array['chapters'][$a]['subject'][$b] = array( "title" => (string) $item );

下一次-它会像这个

$array['chapters'][$a]['subject'][$b]['subject'][$c] = array( "title" => (string) $item );

然后,

$array['chapters'][$a]['subject'][$b]['subject'][$c]['subject'][$d] = array( "title" => (string) $item );

和,

$array['chapters'][$a]['subject'][$b]['subject'][$c]['subject'][$d]['subject'][$e] = array( "title" => (string) $item );

和,

那么,有可能做这样的事情吗?

$superArray = "$array['chapters'][$a]['subject'][$b]"

下一次,

superArray += "['subject'][$c]"

欢迎所有帮助。


这一切的原因是:我从下一个代码块中生成一个递归函数,因为它总是有相同的模式:+我正在将一个XML文件解析为数组,以便稍后在drupal 中使用它

$resulty = $xpaths->xpath("/org.olat.course.Structure/rootNode/children/*[ident = '$item->ident']/children/*[type = 'st' or type = 'sp' or type = 'bc']");
if ($resulty > 0) {
foreach ($resulty as $itemy) {
  $x = 0;
  $array['chapters'][$s]['subject'][$d] = array(
    'id' => (string) $itemy->ident,
    'shortTitle' => (string) $itemy->shortTitle,
    'type' => (string) $itemy->type,
  );
  switch ($itemy->type) {
    case "bc":
      $course_map = getDirectoryList("/opt/olat/olatdata/bcroot/course/$course/foldernodes/$item->ident");
      for ($i = 0; count($course_map) > $i; $i++) {
        $location = "/opt/olat/olatdata/bcroot/course/$course/foldernodes/$item->ident/$course_map[$i]";
        $array['chapters'][$s]['subject'][$d]['files'][$x] = array(
          "name" => $course_map[$i],
          "location" => $location,
          "size" => format_bytes(filesize($location)),
          "type" => filetype($location),
          "modified" => date("F d Y H:i:s.", filemtime($location))
        );
      }
      break;
    case "sp" || "st" :
      $resultz = $xpaths->xpath("//*[ident = '$itemy->ident']/moduleConfiguration/config//string[starts-with(.,'/')]");
      foreach ($resultz as $itemz) {
        $array['chapters'][$s]['subject'][$d] += array(
          "html-page" => (string) $itemz,
        );
      }
      break;
  }
                        <<<--- When you go deeper, than all this wil be set here
  $d++;
   $x++;
}
}

我不确定这是否是你的意思,但如果你想跟踪最后一个"超级数组"并向其添加项目,你可以通过跟踪对最后一个超级数组的引用(&)来做到这一点。

$input = array($b, $c, $d, $e);
$result = array('chapters' => array($a => array()));
$current = &$result['chapters'][$a];
foreach ($input as $iter) {
    $current['subject'][$iter] = array( "title" => (string) $iter );
    $current = &$current['subject'][$iter];
}