PHP-将嵌套数组打印为无序列表

时间:2019-09-09 03:34:20

标签: php arrays

我有以下启发式数组:

   Array
(
    [0] => stdClass Object
        (
            [name] => MD
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => Year 1
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [name] => Integrated Medical Sciences 1...

我想按照继承关系将数组中的名称打印为无序列表。我尝试过:

function walk($array){  
    foreach ($array as $key => $value) {
        echo "<ul>";
        if(!is_array($value->name)){
            echo "<li>$key:[$value->name]</li>";

            walk($value);
        }
        echo "</ul>";
    }
}

walk($roots);

但是请获取以下内容,其中包含正确的名称和关系,但也包含错误:

0:[MD]

    Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
    name:[]
    Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63 

    Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
    children:[]
        0:[Year 1]
            Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
            name:[]
            Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63 
            Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
            children:[]
                0:[Integrated Medical Sciences 1]
                    Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
                    name:[]

第63行是foreach ($array as $key => $value) {

第65行是if(!is_array($value->name)){

第66行是echo "<li>$key:[$value->name]</li>";

更新

可以尝试:

function walk($array){  
    foreach ($array as $key => $value) {
        echo "<ul>";
        if(!is_array($value->name)){
            echo "<li>$key:[$value->name]</li>";
if (isset($value->children)) {
            walk($value->children);
}
        }
        echo "</ul>";
    }
}

walk($roots);

哪个会产生:

0:[MD]

    0:[Year 1]
        0:[Integrated Medical Sciences 1]
        1:[Integrated Medical Sciences 2]

    1:[Year 2]
        0:[Integrated Medical Practice 1]
            0:[Centralised Teaching]
                0:[Seminar - General Medicine Student Led Presentations]
                1:[Surgery - CBL]
            1:[Clinical Preparation]
                0:[Clinical Skills - Respiratory History Taking]
                1:[Seminar - Airways Disease]
                2:[Seminar - Approach to Head Injury and Altered Mental State]
                3:[Seminar - Atherothrombosis]
                4:[Seminar - Breast Cancer]
                5:[Seminar - Collapse]
                6:[Seminar - Cough]
                7:[Seminar - CVS Examination]
                8:[Seminar - Fatigue]
                9:[Seminar - GIT Examination]
                10:[Seminar - MSK Examination]
                11:[Seminar - Overview of CVS Disease]
                12:[Seminar - Overview of Gastroenterology ]
                13:[Seminar - Overview of Kidney and Urological Disease]
                14:[Seminar - Overview of Neurological Disease ]
                15:[Seminar - PVD]
                16:[Seminar - Spinal pain]
                17:[Seminar - Stroke]
                18:[TBL - Cardiac Conduction]
                19:[TBL - CRC]
                20:[TBL - Day 2 post ACS]
                21:[TBL - Infectious Gastro ]
                22:[TBL - Liver Disease]
                23:[TBL - Polyarthritis]
                24:[TBL - Sepsis]
                25:[TBL - Trauma and Fracture]
                26:[TBL - VTE]
                27:[Tutorial - Clinical Skills - Gastrointestinal History Taking]
                28:[Tutorial - CVS Chest Pain and Associated Symptoms]
            2:[Geriatric Medicine]
            3:[Internal Medicine]
                0:[RPH - Bedside Tutorial - Gastro]
                1:[RPH - Bedside Tutorials CVS and Resp]

哪个很棒。但是我只希望名称是文本,而不是数字和方括号...

更新

这是我最终使用的:

function walk($array)
{    
    //convert object to key-value array
    if (is_object($array)) {
        $array = (array)$array;
    }

    echo "<ul>";
    foreach ($array as $key => $value) {
        if (is_int($value) || is_string($value)) {
            echo "<li>" . $value;
        } elseif (is_array($value) || is_object($value)) {
            walk($value);
        }
        echo "</li>";
    }
    echo "</ul>";
}

walk($roots);

1 个答案:

答案 0 :(得分:1)

您的walk函数假设输入是一个数组(实际上是键值数组: foreach($ array as $ key => $ value)),并且此数组中的值应该是对象( $ value->名称)。

您可能应该将功能更改为以下内容:

/**
* @param array|\stdClass $array
*/
function walk($array)
{    
    //convert object to key-value array
    if (is_object($array)) {
        $array = (array)$array;
    }

    echo "<ul>";
    foreach ($array as $key => $value) {
        echo "<li>";
        echo $key;
        if (is_int($value) || is_string($value)) {
            echo ":" . $value;
        } elseif (is_array($value) || is_object($value)) {
            walk($value);
        }
        echo "</li>";
    }
    echo "</ul>";
}