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 8397361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:46:05+00:00 2026-06-09T20:46:05+00:00

I am going to get results from database like this. array( [0] => array(

  • 0

I am going to get results from database like this.

 array(
         [0] => array( 
                       [type]   => orange,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [1] => array( 
                       [type]   => mango,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [1] => array( 
                       [type]   => apple,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [3] => array( 
                       [type]   => mango,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 

     )

Basically, I need to convert this to the following JSON Format:

         "mango" : [{ 
                       "number" : "10",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                       "number" : "12",
                       "size"   : "14c",
                       "weight" : "12"
                     }
                   ],
         "orange" : [{ 
                       "number" : "12",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                       "number" : "12",
                       "size"   : "14c",
                       "weight" : "11"
                     }
                   ],
         "apple" : [{ 
                       "number" : "10",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                   ]

Ignore the number, size and weight. Those are just random numbers.
But the first element in the array contains the JSON tag. That should be removed from the array, but taken as tag for array of objects in JSON (you can see that).

I know we can write with loops, conditions, etc. Is there a smart and quick way to do this with existing built in php functions with less looping and conditions?

I guess, these are the steps:

  1. Filter the arrays individuality based on type. Probably, 3 times here to get the 3 fruits. Which function would help here? Filter based on Value. Or ideally key value pair.
  2. You get 3 arrays, each for one type of fruit. Pop off the type element in all arrays individually.
  3. Loop through the types, and put the arrays in each type, with type value as index.

Any other better ways?

  • 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-09T20:46:06+00:00Added an answer on June 9, 2026 at 8:46 pm

    Restructure your old array to a new one like this :

    $new = array();
    foreach($old as $vec){
        $new[$vec['type']] = array(
            'number' => $vec['number'],
            'size' => $vec['size'],
            'weight' => $vec['weight']      
        );
    }
    echo json_encode($new);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to be able to retrieve results from this search by connecting
I have a loop going which adds results from my database. The values are
am going to read 5- Contacts , So i get this code and go
This seems like a very simple problem, but I cannot get a scrollbox to
I am writing a prototype to programmatically get video data from a database and
I realize I'm going to get flamed for not simply writing a test myself...
Trying to setup and get going with the RestKit library for a cocoa project
I need some help, and before we get going, I know it is probably
I'm starting out to learn about AMQP and RabbitMQ. To get myself going I
I'm going insane trying to get an AVCaptureSession (in a view controller) to be

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.