PHP多维数组到HTML嵌套列表中?

时间:2015-05-19 20:48:37

标签: php html arrays loops html-lists

我有一个数组,其数据嵌套在下面(只是一个简化的例子,因为它必须是“无穷无尽的”,因为每个族树可以超过X代):

Array
(
    [id] => 121
    [name] => Very Important Dogs Bowey Showey
    [gen] => 0
    [mother] => Array
        (
            [id] => 128
            [name] => Veridique Of Winners
            [gen] => 1
            [mother] => 
            [father] => 
        )

    [father] => Array
        (
            [id] => 124
            [name] => Rosickys Robocopico
            [gen] => 1
            [mother] => Array
                (
                    [id] => 75
                    [name] => Astro Spice
                    [gen] => 2
                    [mother] => 
                    [father] => 
                )

            [father] => Array
                (
                    [id] => 62
                    [name] => Your King of the World
                    [gen] => 2
                    [mother] => 
                    [father] => 
                )

        )

)

我尝试使用这个PHP函数但没有成功,因为它确实遍历每个数组并创建一个项目符号点,但我不能将“名称”和“ID”隔离在一起。

function recurseTree($var){
  $out = '<li>';
  foreach($var as $v){
    if(is_array($v)){
      $out .= '<ul>'.recurseTree($v).'</ul>';
    }else{
      $out .= $v;
    }
  }
  return $out.'</li>';
}

echo '<ul>'.recurseTree($familytree).'</ul>';

这就是它给我的东西:

- 121Very Important Dogs' Bowey Showey0
    - 128Veridique Of Winners1
    - 124Rosicky's Robocopico1
        - 75Astra's Spice2
        - 62Lazhar's King of the World2

它给了我一个无法使用的字符串。相反,我希望使用类似$v['id']的内容来获取ID(并创建一个href链接),并使用类似$v['name']的名称......但是如何?!

我所追求的格式:

  <ul>
    <li><span>Mother</a></span>
      <ul>
        <li><span><a href="#">Grandmother</a></span>
          <ul>
            <li><span>Grand-Grandmother</span></li>
            <li><span>Grand-Grandfather</span></li>
          </ul>
        </li>
        <li><span><a href="#">Grandfather</a></span>
      </ul>
    </li>
    <li><span>Father</a></span>
      <ul>
        <li><span><a href="#">Grandmother</a></span>
        <li><span><a href="#">Grandfather</a></span>
      </ul>
    </li>
  </ul>

2 个答案:

答案 0 :(得分:0)

以下是您可以做的事情:

function recurseTree($var)
{
    $out = '<li>';
    foreach($var as $k => $v)
    {
        if(is_array($v))
        {
            $out .= '<ul>'.recurseTree($v).'</ul>';
        }
        else
        {
            switch($k)
            {
                case 'id':
                    $out .= 'id='.$v;
                break;

                case 'name':
                    $out .= 'name='.$v;
                break;
            }
        }
    }

    return $out.'</li>';
}

echo '<ul>'.recurseTree($familytree).'</ul>';

答案 1 :(得分:0)

因为你的数组是关联的并且似乎遵循一致的格式,我认为你实际上可以在不使用循环的情况下实现这一点。这应该使格式化更容易。

function recurseTree($var) {
   if ($var) {
     $link = '<span><a href="link.php?id='. $var['id']. '">'. $var['name']. '</a></span>';
     $parents = '';
     if ($var['mother'] || $var['father']) {
        $parents = '<ul>'. recurseTree($var['mother']). recurseTree($var['father']). '</ul>';
     }
     return "<li>$link$parents</li>";
   }
}
echo '<ul>' . recurseTree($familytree) . '</ul>';