I have a outsource data from :
http://example.com/data/news.json
Here is the example result after decoding :
Array
(
[popular] => Array
(
[last_week] => Array
(
[0] => Array
(
[title] => Business 1
[category] => blog/business/local
)
[1] => Array
(
[title] => Health 1
[category] => blog/health/skincare
)
[2] => Array
(
[title] => Business 2
[category] => blog/business/local
)
[3] => Array
(
[title] => Health 2
[category] => blog/health/skincare
)
)
)
)
I'm use following method to display it :
$url = 'http://example.com/data/news.json';
$json = file_get_contents($url);
if(!empty($json)) {
$json_data = json_decode($json, true);
$popular_last_week = $json_data['popular']['last_week'];
$count = count($popular_last_week);
$result .= $count.' last week popular article' . "\n";
for ($i = 0; $i <$count; $i++) {
$result .= 'Title : '.$popular_last_week[$i]['title'] . "\n";
$result .= 'Category : '.$popular_last_week[$i]['category'] . "\n\n";
}
echo $result;
}
and the output data is :
4 last week popular articles
Title : Business 1
Category : blog/business/local
Title : Health 1
Category : blog/health/skincare
Title : Business 2
Category : blog/business/local
Title : Health 2
Category : blog/health/skincare
The question is how to display the output to be following :
2 last week popular Business articles
Title : Business 1
Category : Busines
Title : Business 2
Category : Business
2 last week popular Health articles
Title : Health 1
Category : Health
Title : Health 2
Category : Health
help would be greatly appreciated! thank you.
titlecontainsBusinessand display it?