Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将此数组转换为HTML列表_Php_Arrays - Fatal编程技术网

Php 将此数组转换为HTML列表

Php 将此数组转换为HTML列表,php,arrays,Php,Arrays,我有这个阵列: array 0 => string '3,6' (length=3) 3 => string '4,5' (length=3) 4 => string '7,8' (length=3) 8 => string '9' (length=1) 或 每个键都是此父项的子项的id,值都是此父项的子项的id。 ID为0表示(3和6)没有父项。 现在,我想输出一个HTML列表,如下所示: 三, 四, 七, 八, 九, 五, 六, $a

我有这个阵列:

array
  0 => string '3,6' (length=3)
  3 => string '4,5' (length=3)
  4 => string '7,8' (length=3)
  8 => string '9' (length=1)

每个
都是此父项的子项的id,
都是此父项的子项的id。
ID为0表示(3和6)没有父项。
现在,我想输出一个HTML列表,如下所示:

  • 三,
    • 四,
      • 七,
      • 八,
        • 九,
    • 五,
  • 六,
$arr=array(
0 => '3,6',
3 => '4,5',
4 => '7,8',
8 => '9',
);
函数writeList($items){
全球$arr;
回声“
    ”; $items=分解(“,”,$items); foreach($items作为$item){ 回显“
  • ”.$item; if(isset($arr[$item])) 写列表($arr[$item]); 回音“
  • ”; } 回声“
”; } 写列表($arr[0]);

$arr=array(
3=>数组(
4=>数组(
7=>null,
8=>数组(
9=>null
),
),
5=>null,
),
6=>null,
);
函数writeList($items){
如果($items==null)
返回;
回声“
    ”; foreach($items作为$item=>$children){ 回显“
  • ”.$item; 作家(儿童); 回音“
  • ”; } 回声“
”; } 写作者($arr);
$arr=array(
0 => '3,6',
3 => '4,5',
4 => '7,8',
8 => '9',
);
函数writeList($items){
全球$arr;
回声“
    ”; $items=分解(“,”,$items); foreach($items作为$item){ 回显“
  • ”.$item; if(isset($arr[$item])) 写列表($arr[$item]); 回音“
  • ”; } 回声“
”; } 写列表($arr[0]);

$arr=array(
3=>数组(
4=>数组(
7=>null,
8=>数组(
9=>null
),
),
5=>null,
),
6=>null,
);
函数writeList($items){
如果($items==null)
返回;
回声“
    ”; foreach($items作为$item=>$children){ 回显“
  • ”.$item; 作家(儿童); 回音“
  • ”; } 回声“
”; } 写作者($arr);
采用这种格式:

$data = array(
    3 => array(
        4 => array(
            7 => null,
            8 => array(
                9 => null
            )
        ),
        5 => null
    ),
    6 => null
);
这样做:

function writeList($tree)
{
    if($tree === null) return;
    echo "<ul>";
    foreach($tree as $node=>$children)
        echo "<li>", $node, writeList($children), '</li>';
    echo "</ul>";
}

writeList($data);
函数写列表($tree)
{
如果($tree==null)返回;
回声“
    ”; foreach($node=>$children的树) echo“
  • ”,$node,writeList($children),“
  • ”; 回声“
”; } 写列表(数据);
在此处测试:

采用以下格式:

$data = array(
    3 => array(
        4 => array(
            7 => null,
            8 => array(
                9 => null
            )
        ),
        5 => null
    ),
    6 => null
);
这样做:

function writeList($tree)
{
    if($tree === null) return;
    echo "<ul>";
    foreach($tree as $node=>$children)
        echo "<li>", $node, writeList($children), '</li>';
    echo "</ul>";
}

writeList($data);
函数写列表($tree)
{
如果($tree==null)返回;
回声“
    ”; foreach($node=>$children的树) echo“
  • ”,$node,writeList($children),“
  • ”; 回声“
”; } 写列表(数据);


在这里测试:

为什么它们是字符串而不是数组?我们可以使用
分解将它们转换为数组。。。我有各种形式的数组,但无法从中生成此列表。e、 g.我在问题中添加了另一个这样的表格。@Eric它类似,但不是重复的。这个问题处理的是子->父关系,而不是父->子关系。@Stoic:如果这样做没有额外的开销,就使用第二种形式。是的,第二种形式更可取。如果这是你必须要开始的,我不知道你为什么会选择另一个方向。为什么它们是字符串而不是数组?我们可以用
分解
将它们转换为数组。。。我有各种形式的数组,但无法从中生成此列表。e、 g.我在问题中添加了另一个这样的表格。@Eric它类似,但不是重复的。这个问题处理的是子->父关系,而不是父->子关系。@Stoic:如果这样做没有额外的开销,就使用第二种形式。是的,第二种形式更可取。如果这是你必须开始的,我不知道你为什么会选择另一个方向。我本来打算把它分为树构建和html输出,但我喜欢你在这里的工作方式。非常简洁谢谢你,阿林。。你的功能非常好。。不过,仅仅接受Eric的答案,因为它为我节省了一个中间函数,我正在使用:)嗯。。。我为您第一次发布的格式制作了第一个函数。我也为第二种格式添加了一个,但作为标准实现,它几乎与Eric的+1完全相同。我本来打算把它分为树构建和html输出,但我喜欢你在这里的工作方式。非常简洁谢谢你,阿林。。你的功能非常好。。不过,仅仅接受Eric的答案,因为它为我节省了一个中间函数,我正在使用:)嗯。。。我为您第一次发布的格式制作了第一个函数。我也为第二种格式添加了一个,但作为一个标准实现,它几乎与Eric的完全相同。@Alin:oops。我刚贴出来就发现了!非常感谢。这才是我想要的真正的功能。。简单、甜美、简洁……:)添加了一些警告检查以满足我的需要(100%:)我可以问一下需要什么警告检查吗?@Stoic:等等,你真的选择将你的继承人数组变成一个噩梦般地狱般的表示数组索引的字符串?@Eric。。哦,是的。我确实做到了。因为,我根本不知道如何遍历一棵树(递归是非常基本的事情之一,我们需要知道,我就是不知道)@Alin:oops。我刚贴出来就发现了!非常感谢。这才是我想要的真正的功能。。简单、甜美、简洁……:)添加了一些警告检查以满足我的需要(100%:)我可以问一下需要什么警告检查吗?@Stoic:等等,你真的选择将你的继承人数组变成一个噩梦般地狱般的表示数组索引的字符串?@Eric。。哦,是的。我确实做到了。因为,我根本不知道如何遍历一棵树(递归是最基本的事情之一,我们需要知道,我就是不知道)
function writeList($tree)
{
    if($tree === null) return;
    echo "<ul>";
    foreach($tree as $node=>$children)
        echo "<li>", $node, writeList($children), '</li>';
    echo "</ul>";
}

writeList($data);