Hermes
Hermes

Reputation: 462

PHP foreach object error

I used a POST method later I want to add POST request to a variable but I get an error. Why I get object error?

Error code

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

POST data

array(16) { ["lat"]=> string(15) "41.008550564147" ["lng"]=> string(15) "28.978239484058" ["time"]=> string(1) "6" ["type"]=> string(6) "status" ["status"]=> string(0) "" ["category"]=> string(0) "" ["subCategory"]=> string(0) "" ["room"]=> string(0) "" ["buildingAge"]=> string(0) "" ["sfloor"]=> string(0) "" ["floor"]=> string(0) "" ["square"]=> string(0) "" ["price"]=> string(0) "" ["currency"]=> string(0) "" ["value"]=> string(0) "" ["feedback"]=> string(0) "" }

PHP code

    foreach ($_POST as $key => $value) {
        $params .= $key . "=" . $value . "&";
    }
    echo $params;

Upvotes: 0

Views: 76

Answers (1)

bassxzero
bassxzero

Reputation: 5041

Use http_build_query instead.

echo http_build_query($_POST);

Upvotes: 1

Related Questions