Guys any help is appreciated. New to php & learning as I go. My Objective: change the array to an object and print it out as a table. Below is my json data which is save as people.txt file.
{
"people": [
{
"first_name": "John",
"last_name": "Smith",
"city": "NY",
"state": "New York"
},
{
"first_name": "Jane",
"last_name": "Smith",
"city": "Paris",
"state": "France"
},
{
"first_name": "John",
"last_name": "Smith",
"city": "Orlando",
"state": "Florida"
},
{
"first_name": "Jane",
"last_name": "Smith",
"city": "Toronto",
"state": "Canada",
}
]
}
//this issue is when I try to change the from array to an object using $people_data = json_decode ($json_data); without the second parameter "true". or even using $people_data = json_decode (json_encode ($json_data)); //HERE IS MY PHP CODE
$json_data = file_get_contents ("people.txt");
// I was able to use $people_data = json_decode ($json_data, true); to output my table just fine by changing my data to an array.
$people_data = json_decode ($json_data, true);
$output = "<table border='1' style=' border-collapse:collapse; 'width=25%'>";
$output .= "<tr>";
$output .= "<th>". "First Name" ."</th>";
$output .= "<th>". "Last Name" ."</th>";
$output .= "<th>". "City" ."</th>";
$output .= "<th>". "State" ."</th>";
$output .= "</tr>";
foreach ($people_data ["people"] as $person) {
$output .= "<tr>";
$output .= "<td>". $person ["first_name"] ."</td>";
$output .= "<td>". $person ["last_name"] ."</td>";
$output .= "<td>". $person ["city"] ."</td>";
$output .= "<td>". $person ["state"] ."</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
?>
this is a pic of my results using an "array", trying to get the same result as "object". json table