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?

7
  • 3
    What have you tried? Commented Aug 17, 2012 at 19:09
  • 1
    You're gonna need some loops... Commented Aug 17, 2012 at 19:10
  • 3
    You're looking to drastically restructure the array before encoding it. This is called programming, and there is no built-in function to do programming for you. Commented Aug 17, 2012 at 19:11
  • 1
    No, such a magic function does not exist. Use loops, conditions etc to change the array structure to the desired, and the use json_encode. Commented Aug 17, 2012 at 19:11
  • 1
    @KevinRave The first step in optimizing your code is to have some code to optimize. If you haven't tried anything yet, what can we help you with? Commented Aug 17, 2012 at 19:13

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@alpera. Lol! 2nd Mango..Anyways, Probably the array will have only one array of each type. But I get the idea. We can write addition step to add instead of overwrite.
0

Change array structure as you need first and then use json_encode().

1 Comment

This is a simple re-wording of his question, with a link to the docs. Not very useful as an answer...

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.