1

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!

1 Answer 1

2

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%
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.