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

7
  • 4
    Could you give some sample data to work with - also what you would expect it to be output as. Commented Sep 6, 2019 at 9:00
  • On which line it's showing that error? Commented Sep 6, 2019 at 9:06
  • See updated OP.. Commented Sep 6, 2019 at 9:11
  • Try to change $result->fetch_object() to $result->fetch_assoc() Commented Sep 6, 2019 at 9:25
  • Are you working with @Tenderfoot? He asked a similar question 15 minutes ago. Commented Sep 6, 2019 at 9:30

4 Answers 4

1

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

                $out .= "<li><span>$key:[$elem->name]</span></li>";
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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>";
    }
}

Comments

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

2 Comments

A.Rosano...where is toString() implemented?
You have to implement it in the classes of objects you extract from DB
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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.