I have a response from a website as
[{"name":"xxx","phone":"123","email":"[email protected]"},{"name":"yyy","phone":"456","email":"[email protected]"},{"name":"zzz","phone":"678","email":"[email protected]"}...]
My code is
$json = '[{"name":"xxx","phone":"123","email":"[email protected]"},{"name":"yyy","phone":"456","email":"[email protected]"},{"name":"zzz","phone":"678","email":"[email protected]"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
...
}
When I run this code, I get an error Invalid argument supplied for foreach()
What is the issue here?
How can I show as table in html table?
this code works perfectly fine - just testetd it on a local machine
<?php
$json = '[{"name":"xxx","phone":"123","email":"[email protected]"},{"name":"yyy","phone":"456","email":"[email protected]"},{"name":"zzz","phone":"678","email":"[email protected]"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
print_r($result);
}
?>
To output your stuff as table, use this:
<?php
$json = '[{"name":"xxx","phone":"123","email":"[email protected]"},{"name":"yyy","phone":"456","email":"[email protected]"},{"name":"zzz","phone":"678","email":"[email protected]"}]';
$json_decoded = json_decode($json);
echo '<table>';
foreach($json_decoded as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
echo '</table>';
?>
Make sure that your JSON string doesn't have any syntax errors... if yes the json_decode will fail and the foreach() loop throws an error.
Collected from the Internet
Please contact javaer1[email protected] to delete if infringement.
Comments