I'm very new to PHP and I need your help! I need to write backend for my app that receives json post and write data to json file. And I'm stuck with looping through array.
$postData = file_get_contents("php://input");
$request = json_decode($postData);
var_damp($request)
shows array:
array(2) {
[0]=>
object(stdClass)#1 (8) {
["name"]=>
string(11) "Alex Jordan"
["email"]=>
string(14) "[email protected]"
["phone"]=>
int(123456789)
["street"]=>
string(12) "street, str."
["city"]=>
string(7) "Chicago"
["state"]=>
string(7) "Chicago"
["zip"]=>
string(5) "07202"
["$$hashKey"]=>
string(8) "object:3"
}
[1]=>
object(stdClass)#2 (8) {
["name"]=>
string(15) "Michael Jonhson"
["email"]=>
string(17) "[email protected]"
["phone"]=>
float(11987654321)
["street"]=>
string(12) "street, str."
["city"]=>
string(11) "Los Angeles"
["state"]=>
string(10) "California"
["zip"]=>
string(5) "27222"
["$$hashKey"]=>
string(8) "object:4"
}
}
I'm trying to loop through the objects and getting error
Object of class stdClass could not be converted to string
Here is how I'm trying to do it:
foreach($request as $i => $i_value) {
echo $i_value;
}
echo $i_value->name;
etc... Or foreach the object as well. – Vernice