I have this string
$search_data = '{"Search Engine":"High Schools","Population":300,"Avg GPA":"3.2","Graduation Rate":"98%"}';
$data = explode(",", $search_data);
$display_data = "<br/>";
foreach($data as $item){
$display_data .= "<span>$item</span><br/>";
}
echo $display_data;
What is echoed looks like this:
{"Search Engine":"High Schools"
"Population":300
"Avg GPA:"3.2"
"Graduation Rate":"98%"}
How can i properly format this so the displayed text looks like this:
Search Engine: High Schools
Population : 300
Avg GPA: 3.2
Graduation Rate: 98%
Any helpful tips will be appreciated - Thanks!
CodePudding user response:
As the data is JSON you can just decode it to an assocative array. Then foreach over to print each key and value.
$search_data = '{"Search Engine":"High Schools","Population":300,"Avg GPA":"3.2","Graduation Rate":"98%"}';
foreach (json_decode($search_data, true) as $key => $value) {
echo "$key: $value\n";
}
Output
Search Engine: High Schools
Population: 300
Avg GPA: 3.2
Graduation Rate: 98%