0

Trying to print a nested array as a list:

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

Produces error

Catchable fatal error: Object of class stdClass could not be converted to string on line

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

So its an object, but what should I be doing to fix this?

Array is like:

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

                    [1] => stdClass Object
                        (
                            [name] => Year 2
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [name] => Integrated Medical Practice 1
                                            [children] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [name] => Centralised Teaching
                                                            [children] => Array
                                                                (
                                                                    [0] => stdClass Object
                                                                        (
                                                                            [name] => Seminar - General Medicine Student Led Presentations
                                                                        )

                                                                    [1] => stdClass Object
                                                                        (
                                                                            [name] => Surgery - CBL
                                                                        )
                                                                )
                                                        )

UPDATE

Tried the following:

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)

which produces:

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:[]
                    Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63 
                1:[Integrated Medical Sciences 2]
                    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 
        1:[Year 2]
            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 Practice 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 66 is echo "<li>$key:[$value->name]</li>";

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100

4 Answers4

1

The elements of your arrays are objects, you need to print the name property.

                $out .= "<li><span>$key:[$elem->name]</span></li>";
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You should post the solution in an answer, not the question. And don't accept my answer if it didn't really solve the problem. – Barmar Sep 10 '19 at 14:51
1

$elem probably is an object. When you check type of $elem

    if(!is_array($elem)){
            $out .= "<li><span>$key:[$elem]</span></li>";
    }
    else $out .= "<li><span>$key</span>".array2ul($elem)."</li>";

you need to check is_object($elem) also. A var_dump of an object result in an array with first element:

[0] => stdClass Object

First of all you have to implement a __toString() method in the classes for auto-conversion of an object of those classes to a string. Where and why do we use __toString() in PHP?

After that you can simply:

    if (is_array($elem))
        $out .= "<li><span>$key</span>".array2ul($elem)."</li>";
    else
        $out .= "<li><span>$key:[$elem]</span></li>";

because the __toString() of the classes is implemented

A. Rosano
  • 46
  • 4
1
foreach($array as $key => $elem){
    if(is_array($elem)){
        $out .= "<li><span>$key</span>".array2ul($elem)."</li>";
    }
    elseif(is_object($elem)){
        $out .= "<li><span>$key:[$elem->name]</span></li>";
        $out .= "<li><span>$key</span>".array2ul($elem->children)."</li>";
    }
    else{
        $out .= "<li><span>$key:[$elem]</span></li>";
    }
}
0

I got things working with this:

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

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

    // Here we return result array
    return $pdf_result;
}

walk($roots);

// And here we set another-scope variable from function
$pdf_result = walk($roots);
IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100