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

  • Home
  • SEARCH
  • 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 8447433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:10:42+00:00 2026-06-10T10:10:42+00:00

I have a large JSON file with more than 200 items. There are 5

  • 0

I have a large JSON file with more than 200 items. There are 5 groups data in it. each group has at least 30 items. I distinguish them using "p" tag, like "p":"1","p":"2","p":"3","p":"4","p":"5" in the json data. Now I want get 2 items from each group and total for 10 items with a random orders.

For an easy explanation, I set some simple data like below. “p”:”1″ have 4 items in its group, “p”:”2″ have 4 items in its group.

Now how to make get 2 items from "p":"1" and 2 items with "p":"2" with a random orders?

$json = <<<ETO
[
   {
      "a":"apple",
      "p":"1"
   },
   {
      "a":"orange",
      "p":"1"
   },
   {
      "a":"pear",
      "p":"1"
   },
   {
      "a":"banana",
      "p":"1"
   },
   {
      "a":"Chauli",
      "p":"2"
   },
   {
      "a":"Carrot",
      "p":"2"
   },
   {
      "a":"Lettuce",
      "p":"2"
   },
   {
      "a":"Potato",
      "p":"2"
   }
]
ETO;

$data = json_decode($json);
shuffle($data);// some shuffle like this is very ugly...
foreach($data as $row){                         
      $aoo = 1;
      $boo = 1;
      if($row->p==1){
          echo $row->p.': '.$row->a.'<br />';
          $aoo++;
          if($aoo==2){
            break;  
          }
      }
      if($row->p==2){
          echo $row->p.': '.$row->b.'<br />';
          $boo++;
          if($boo==2){
            break;  
          }
      }
}

I need get the result like:

orange ("p":"1") Carrot ("p":"2") pear ("p":"1") Lettuce ("p":"2")

Lettuce ("p":"2") banana ("p":"1") Apple ("p":"1") Chauli ("p":"2")

In one word, get 2 Vegetables and 2 Fruits combine for a mixed orders. Thanks.

  • 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-10T10:10:43+00:00Added an answer on June 10, 2026 at 10:10 am
    $values = json_decode($json, true);
    
    $grouped = array();
    foreach ($values as $value) {
        $grouped[$value['p']][] = $value;
    }
    
    $chosen = array();
    foreach ($grouped as $group) {
        $keys = array_rand($group, 2);
        $chosen = array_merge($chosen, array_intersect_key($group, array_flip($keys)));
    }
    
    shuffle($chosen);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large JSON file that contains A LOT of similar code. It's
So, I have a file with a large JSON array of objects, and unfortunately,
I need to get rather complicate JSON data from (large) JSON file into a
I have a very large .json file on disk. I want to instantiate this
I have a file with data in the json format. The data is odd
How to store large JSON String in cookie ? I have to use cookie
I have large table, with around 200 fields. Around a 100 of those fields
I have a large nested array that I'm generating from parsing a CSV file
I need to pre-compress some very large html/xml/json files (large data dumps) using either
I have a huge chunk, so large that I can't manually edit the file

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.