将嵌套数组打印为列表

尝试将嵌套数组打印为列表:


$result = $connection->query($query);

$data = array();

while ($row = $result->fetch_object()) {

    $data[$row->global_id] = $row;

}


$roots = array();

foreach ($data as $row) {   

    if ($row->parent_global_id === null) {

        $roots[]= $row;

    } else {

        $data[$row->parent_global_id]->children[] = $row;

    }

    unset($row->parent_global_id);

    unset($row->global_id);

}


function array2ul($array) {

    $out = "<ul>";

    foreach($array as $key => $elem){

        if(!is_array($elem)){

                $out .= "<li><span>$key:[$elem]</span></li>";

        }

        else $out .= "<li><span>$key</span>".array2ul($elem)."</li>";

    }

    $out .= "</ul>";

    return $out; 

}


array2ul($roots)

产生错误


可捕获的致命错误:无法在线将类 stdClass 的对象转换为字符串


$out .= "<li><span>$key:[$elem]</span></li>";

所以它是一个对象,但我应该怎么做才能解决这个问题?


数组是这样的:


Array

(

    [0] => stdClass Object

        (

            [name] => MD

            [children] => Array

                (

                    [0] => stdClass Object

                        (

                            [name] => Year 1

                            [children] => Array

                                (

                                    [0] => stdClass Object

                                        (

                                            [name] => Integrated Medical Sciences 1

                                        )


                                    [1] => stdClass Object

                                        (

                                            [name] => Integrated Medical Sciences 2

                                        )

                                )

                        )

慕莱坞森
浏览 212回答 3
3回答

收到一只叮咚

数组的元素是对象,您需要打印name属性。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$out&nbsp;.=&nbsp;"<li><span>$key:[$elem->name]</span></li>";

森林海

$elem大概是一个对象。当你检查类型$elem&nbsp; &nbsp; if(!is_array($elem)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $out .= "<li><span>$key:[$elem]</span></li>";&nbsp; &nbsp; }&nbsp; &nbsp; else $out .= "<li><span>$key</span>".array2ul($elem)."</li>";你也需要检查is_object($elem)。对象的 var_dump 会生成一个包含第一个元素的数组:[0] => stdClass Object首先,您必须在类中实现一个 __toString() 方法,以便将这些类的对象自动转换为字符串。 我们在哪里以及为什么在 PHP 中使用 __toString() ?之后,您可以简单地:&nbsp; &nbsp; if (is_array($elem))&nbsp; &nbsp; &nbsp; &nbsp; $out .= "<li><span>$key</span>".array2ul($elem)."</li>";&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; $out .= "<li><span>$key:[$elem]</span></li>";因为__toString()类的实现

一只甜甜圈

我得到了这样的事情:function walk($array){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //convert object to key-value array&nbsp; &nbsp; if (is_object($array)) {&nbsp; &nbsp; &nbsp; &nbsp; $array = (array)$array;&nbsp; &nbsp; }&nbsp; &nbsp; $pdf_result = "<ul>";&nbsp; &nbsp; foreach ($array as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; if (is_int($value) || is_string($value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pdf_result .=&nbsp; "<li>" . $value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } elseif (is_array($value) || is_object($value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pdf_result .= walk($value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $pdf_result .=&nbsp; "</li>";&nbsp; &nbsp; }&nbsp; &nbsp; $pdf_result .=&nbsp; "</ul>";&nbsp; &nbsp; // Here we return result array&nbsp; &nbsp; return $pdf_result;}walk($roots);// And here we set another-scope variable from function$pdf_result = walk($roots);
打开App,查看更多内容
随时随地看视频慕课网APP