简体   繁体   中英

Iterating through Array in PHP

I am trying below array to populate in but getting error: Catchable fatal error: Object of class stdClass could not be converted to string in D:\\Apache24\\htdocs\\services\\xxxx.php on line 40 Array ( [0] => stdClass Object ( [actor_id] => 2 [first_name] => NICK [last_name] => WAHLBERG [last_update] => 2006-02-15 04:34:33 )

[1] => stdClass Object
    (
        [actor_id] => 3
        [first_name] => ED
        [last_name] => CHASE
        [last_update] => 2006-02-15 04:34:33
    )

[2] => stdClass Object
    (
        [actor_id] => 4
        [first_name] => JENNIFER
        [last_name] => DAVIS
        [last_update] => 2006-02-15 04:34:33
    )

[3] => stdClass Object
    (
        [actor_id] => 5
        [first_name] => JOHNNY
        [last_name] => LOLLOBRIGIDA
        [last_update] => 2006-02-15 04:34:33
    )

[4] => stdClass Object
    (
        [actor_id] => 6
        [first_name] => BETTE
        [last_name] => NICHOLSON
        [last_update] => 2006-02-15 04:34:33
    )

[5] => stdClass Object
    (
        [actor_id] => 7
        [first_name] => GRACE
        [last_name] => MOSTEL
        [last_update] => 2006-02-15 04:34:33
    )

[6] => stdClass Object
    (
        [actor_id] => 8
        [first_name] => MATTHEW
        [last_name] => JOHANSSON
        [last_update] => 2006-02-15 04:34:33
    )

[7] => stdClass Object
    (
        [actor_id] => 9
        [first_name] => JOE
        [last_name] => SWANK
        [last_update] => 2006-02-15 04:34:33
    )

[8] => stdClass Object
    (
        [actor_id] => 10
        [first_name] => CHRISTIAN
        [last_name] => GABLE
        [last_update] => 2006-02-15 04:34:33
    )

[9] => stdClass Object
    (
        [actor_id] => 11
        [first_name] => ZERO
        [last_name] => CAGE
        [last_update] => 2006-02-15 04:34:33
    )
)

I am using the function below: public function encodeHtml($responseData) {

    $htmlResponse = "<table border='1'>";
    foreach($responseData as $key=>$value) {
            $htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>";
    }
    $htmlResponse .= "</table>";
    return $htmlResponse;       
}

What is wrong I am doing here. Please help me out.

You're trying to print whole object as string here: $htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>"; $htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>";

You should print every object variable separately, eg to print first_name of actor: $htmlResponse .= "<tr><td>". $key. "</td><td>". $value->first_name. "</td></tr>"; $htmlResponse .= "<tr><td>". $key. "</td><td>". $value->first_name. "</td></tr>";

When using foreach from responseData you get each elemt. So the value gonna be a class and when

$htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>";

The $value is a class wich is as the error said can not be converted to a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM