0

I have the following heirachical array:

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

I want to print the names in the array as per the heirachical relationships as an unordered list. I have tried:

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);

But get the following, which contains the correct names and relationships, but also errors:

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:[]

Line 63 is foreach ($array as $key => $value) {

Line 65 is if(!is_array($value->name)){

Line 66 is echo "<li>$key:[$value->name]</li>";

UPDATE

OK tried:

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);

Which produces:

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]

Which is great. But I only want the names as text, not numbers and square brackets...

UPDATE

This is what I ended up using:

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);
8
  • I think you want to walk($value->children); Commented Sep 9, 2019 at 4:09
  • Notice: Undefined property: stdClass::$children in /var/www/html/md/json/generate_json_by_year_print.php on line 68 Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63 Commented Sep 9, 2019 at 4:15
  • changing foreach ($array as $key => $value->children) { makes the line 66 error go away, but still left with the other errors. Commented Sep 9, 2019 at 4:18
  • If the children property might not exist (instead to an empty array - you don't provide enough sample data to know) you will need if (isset($value->children)) walk($value->children); Commented Sep 9, 2019 at 4:19
  • Nick...Thanks! See updated OP. Commented Sep 9, 2019 at 4:24

1 Answer 1

1

Your walk function assumes that the input is an array (a key value array in fact: foreach ($array as $key => $value)) and the values in this array should be objects ($value->name).

You should probably change your function to something like this:

/**
* @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>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that works but I need to have only the names show, not numbers, 'name' and 'children'
By names, you mean you need to print only the keys not the values? (if yes, just comment the echo ":" . $value; line)
No, keep the name vales but not the keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.