I'm new to PHP and I am reading data from external JSON file using PHP and I would like to group the same value (similar to Group by in MySQL). I have tried several ways and research but I could not get the expected result please help.
This is my JSON data:
[{"name":"jonh","school":"Barak Primary school","class":"3rd grade","year":"2019"},{"name":"Danny","school":"Barak Primary school","class":"4rd grade","year":"2019"},{"name":"Ben","school":"Mara Primary school","class":"3rd grade","year":"2019"}{"name":"West","school":"Mara Primary school","class":"3rd grade","year":"2019"}]
And this is my PHP
$json = file_get_contents('https://xxxx.json');//get data from url
$posts = json_decode($json,true); // put data into variable
foreach ($posts as $val) {
echo '<p>' . $val[school] . '</p>';
}
And this is result I got:
Barak Primary School
Barak Primary School
Mara Primary School
Mara Primary School
But I want the result like this:
Barak Primary School
Mara Primary School
So in this case it grouped the same value into one, all your suggestions are appreciated.