PHP打印嵌套类别数组

时间:2016-01-12 17:37:21

标签: php arrays nested

数组是这样的:

array (
  'id' => 10,
  'name' => 'category 1',
  'children' => 
  array (
    0 => 
    array (
      'id' => 11,
      'name' => 'category 1 child',
      'children' => 
      array (
        0 => 
        array (
          'id' => 17,
          'name' => 'category 1 grandchild',
          'children' => 
          array (
            0 => 
            array (
              'id' => 19,
              'name' => 'category 1 great-grandchild',
              'children' => 
              array (
              ),
            ),
          ),
        ),
        1 => 
        array (
          'id' => 20,
          'name' => 'category 1 grandchild 2',
          'children' => 
          array (
          ),
        ),
      ),
    ),
    1 => 
    array (
      'id' => 18,
      'name' => 'category 1 child 2',
      'children' => 
      array (
        0 => 
        array (
          'id' => 21,
          'name' => 'category 1 child 2 grandchild',
          'children' => 
          array (
          ),
        ),
      ),
    ),
  ),

而且:

array (
  'id' => 12,
  'name' => 'category 2',
  'children' => 
  array (
    0 => 
    array (
      'id' => 13,
      'name' => 'category 2 child',
      'children' => 
      array (
        0 => 
        array (
          'id' => 15,
          'name' => 'category 2 grandchild',
          'children' => 
          array (
            0 => 
            array (
              'id' => 16,
              'name' => 'category 2 great-grandchild',
              'children' => 
              array (
              ),
            ),
          ),
        ),
      ),
    ),
    1 => 
    array (
      'id' => 22,
      'name' => 'category 2 child 2',
      'children' => 
      array (
        0 => 
        array (
          'id' => 23,
          'name' => 'category 2 child 2 grandchild',
          'children' => 
          array (
          ),
        ),
        1 => 
        array (
          'id' => 24,
          'name' => 'category 2 child 2 grandchild 2',
          'children' => 
          array (
          ),
        ),
      ),
    ),
  ),
)  

需要打印成嵌套的<ul><li>,到目前为止几乎已经实现了这个:

function printCategory($category, $html='', $space=0, $test=false) {
        \Log::info($category);
        if (count($category['children']) > 0) {
            $html .= '<ul>';
            $html .= '<li>' . $category['name'] . '</li>';

            foreach ($category['children'] as $c) {
                $space++;
                $html = printCategory($c, $html, $space, true);
            }

            if ($test === false) {
                $space += count($category['children']);
                for ($i=0; $i<$space; $i++) {
                    $html .= '</ul>';
                }
            }
        }
        else {
            $html .= '<ul>';
            $html .= '<li>' . $category['name'] . '</li>';
            for ($i=0; $i<$space; $i++) {
                $html .= '</ul>';
                $space--;
            }
        }

        return $html;
    }

产生这个:

<li>category 1</li><ul><li>category 1 child</li><ul><li>category 1 grandchild</li><ul><li>category 1 great-grandchild</li></ul></ul><ul><li>category 1 grandchild 2</li></ul></ul><ul><li>category 1 child 2</li><ul><li>category 1 child 2 grandchild</li></ul></ul>
<li>category 2</li><ul><li>category 2 child</li><ul><li>category 2 grandchild</li><ul><li>category 2 great-grandchild</li></ul></ul><ul><li>category 2 child 2</li><ul><li>category 2 child 2 grandchild</li></ul></ul><ul><li>category 2 child 2 grandchild 2</li></ul></ul>

除了category 2 child 2 grandchild 2关闭category 2 child 2

之外,技术上几乎正确

见图片,假设在function printCategory($category, $html='', $level=0, $test=false) { $html .= '<ul>'; if (count($category['children']) > 0) { $html .= '<li>' . $category['name'] . '</li>'; foreach ($category['children'] as $c) { $html = printCategory($c, $html, $level, true); } } else { $html .= '<li>' . $category['name'] . '</li>'; } $html .= '</ul>'; return $html; } 下。

如果有人能帮助我,请多谢,谢谢。

修改

NVM,用更简单的方法解决:

strcat(msg, "Insert username:\n");

if(write(client, msg, 100) <= 0)
{
perror("Error");
exit(1);
}

if(read(client, name, 30) <= 0)
{
perror("Error");
exit(1);
}
sqlite3_stmt *res;
char sql[100];

sprintf("SELECT * FROM users where name=\"%s\";", name);

int rc = sqlite3_prepare_v2(db, sql, -1, &res, NULL);

if(rc != SQLITE_OK)
{
perror("Error");
exit(1);
}

int step = sqlite3_step(res);
if(step != SQLITE_DONE)
{
perror("Error");
exit(1);
}

2 个答案:

答案 0 :(得分:0)

也许是一个递归函数?

$output = "";

function iterate($array, &$output){
  $output .= “<ul>”;    
  foreach($array as $key => $value){
    if(gettype($value) == “array”){
      iterate($value);
    } else {
      $output .= “<li>{$value}</li>";
    }
  }  
  $output .= “</ul>”;
}

iterate($array, $output);

答案 1 :(得分:0)

代码中的一个问题是你假设在最后一个for循环中,一旦你处于叶子节点,你应该将ul右后方关闭到顶层,但这种情况并非如此。您应该只关闭当前的ul,并让递归函数调用的每个实例自行处理打开和关闭自己的ul标记。

此外,您在if中也有一些代码也出现在else中,所以只需将其放在公共部分。

我不确定您是否希望使用 $ space 变量,但我已将其重新用于缩进HTML标记,以便您可以通过查看制作了HTML。

以下是改编的代码:

function printCategory($category, $html='', $space=0, $test=false) {
    $indent = str_repeat(" ", $space); // generate the spaces for indenting 
    $html .= "$indent<ul>\n"; // include the indenting in every "line"
    $html .= "$indent <li>" . $category['name'] . "</li>\n";
    // IF is not needed as for-loop will only execute if there are children
    foreach ($category['children'] as $c) {
        $html = printCategory($c, $html, $space + 1, true);
    }
    // always close just the one UL we opened at the start of this function.
    // the recursive calls will take care of their own UL tags.
    $html .= "$indent</ul>\n";

    return $html;
}

我使用以上内容在浏览器中测试它:

<?php
    $cats = array ( /* your first array */ );
    $html = htmlentities(printCategory($cats));
    echo "<pre>$html</pre>";
?>

输出:

<ul>
 <li>category 1</li>
 <ul>
  <li>category 1 child</li>
  <ul>
   <li>category 1 grandchild</li>
   <ul>
    <li>category 1 great-grandchild</li>
   </ul>
  </ul>
  <ul>
   <li>category 1 grandchild 2</li>
  </ul>
 </ul>
 <ul>
  <li>category 1 child 2</li>
  <ul>
   <li>category 1 child 2 grandchild</li>
  </ul>
 </ul>
</ul>