制作动态多维数组是否可行?
我想用数组做一个递归函数。 如果我能说出像
那样的话会很棒$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++;
}
}
答案 0 :(得分:0)
我不确定这是不是你的意思,但如果你想跟踪你的最后一个“超级阵列”并添加项目,你可以通过跟踪引用(&)来实现最后一个超级阵列。
$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];
}