Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:29:32+00:00 2026-06-05T19:29:32+00:00

I have a outsource data from : http://example.com/data/news.json Here is the example result after

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:29:34+00:00Added an answer on June 5, 2026 at 7:29 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have one Doubt In MVC Architecture we can able to pass data from Controller
I have a web service and an outsource company will use my service. I
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have searched for the answer but no joy, so here goes... I'm working on
Have an issue with jquery autocomplete, focus function After i type something in the
How to add the distrubtion group from Powershell in Active Directory ? I have
this is really strange, i have an array in delphi and fill it with
I have a CSS file and a PHP file that I received from an
Have a website project in MVC3 C # where I retrieve information from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.