0

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.

2
  • 1
    Not sure if I understand correctly ... Simply iterate through the array, check if title contains Business and display it? Commented Jun 16, 2012 at 6:45
  • Hi, Thanks for your answer I'm new with PHP and it would be great if you give an example. :-D Commented Jun 16, 2012 at 6:50

2 Answers 2

3
$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'];

    //  This loop will group all entries by category.
    $categories = array();
    foreach ($popular_last_week as $item) {
        $categories[$item['category']][] = $item['title'];
    }

    //  This loop will echo the titles grouped by categories.
    foreach ($categories as $category => $titles) {
        $result = count($titles) . ' popular in "' . $category . '"' . "\n";
        foreach ($titles as $title) {
            $result .= 'Title: ' . $title . "\n";
        }
        echo $result;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

the category is taken to be only business
The problem is the title is not always contains "Business".
Let assumsing I've 4 articles (any words) with 2 categories "blog/business/local" and "blog/health/skincare" and I want to display them populated by category.
Wow that exactly what I want, you're rock buddy!! thanks a lot!
0

Do you mean that you only want to show 2 items? You need to use a for loop.

for ($i = 0;  $i < 2; $i++) {
    # display for $i
}

Update:

Ah, I think I understand. You should use a foreach loop and loop through twice:

$categoryItems = array();
foreach ($popular_last_week as $item) {
    $categoryItems[$item['category']][] = $item['title'];
}
foreach ($categoryItems as $category => $items) {
    $result .= count($items) . ' popular in category ' . $category;
    foreach ($items as $item){
        $result .= 'Title: ' . $item['title'] . "\n";
    }
}
echo $result;

3 Comments

Hi, Yes I'm also put that code but I don't know maybe eaten by editor
Please see my updates, sorry it's hard to format when typing on my phone
You know, Pateman copied my answer. You should have accepted mine.

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.