Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this array:

$users = Array
(
[Gareth] => Array
    (
        [25732] => 180
        [25689] => 2310
        [25760] => 
        [25759] => 
        [25758] => 
        [25728] => 
        [25734] => 
    )
[Adam] => Array
    (
        [25732] => 
        [25689] => 
        [25760] => 
        [25759] => 
        [25758] => 420
        [25728] => 60
        [25734] => 
    )
[Cennydd ] => Array
    (
        [25732] => 
        [25689] => 
        [25760] => 
        [25759] => 
        [25758] => 
        [25728] => 
        [25734] => 1035
    )
)

It has users with ids of work and the duration they've spent on that work in minutes.

I need to output this data using highcharts so it needs to be in a Json format.

Currently, using json_encode, it returns:

{ "Gareth":
  {
    "25732":180,"25689":2310,"25760":null,"25759":null,"25758":null,"25728":null,"25734":null
  },
  "Adam Jukes":
  {
    "25732":null,"25689":null,"25760":null,"25759":null,"25758":420,"25728":60,"25734":null
  },
  "Cennydd":
  {
    "25732":null,"25689":null,"25760":null,"25759":null,"25758":null,"25728":null,"25734":1035
  }
}

But I need this in the format:

[{
    "name": 'Gareth ',
    "data": [180, 2310,null, null, null,null, null]
    },
    {   "name": 'Adam',
    "data": [null, null, null,null,420, 60, null]
    },{
    "name": 'Cennydd',
    "data":[null, null , null, null, null,null, 1035 ]
    }]
}]

But I can't seem to figure it out. Do I need to use a foreach to separate and create a new array with the right format?

share|improve this question
1  
Where do you get the first array from ? –  Yellow Bird Mar 19 at 12:48
    
You have to convert php array to the desired format befor encoding it to JSON –  hindmost Mar 19 at 12:51
    
What have you tried to get it in that format? –  ElefantPhace Mar 19 at 12:51
    
you'll have to iterate over your data and reformat a new array from it ... what else ? –  Bixi Mar 19 at 12:51
    
It's a combination of 2 arrays. Because sql wouldn't return a row if they didn't have a duration I use two other arrays to create this array. One array has a list of all the work Ids, the second has the users Id and duration and inputs the duration next to the id if it is present, or leaves it null if not present. The data I'm getting back in my new array is exactly what I need, just not in the correct format. –  Shamrockonov Mar 19 at 12:52

3 Answers 3

up vote 2 down vote accepted

The best way to get what you want would be to modify the way you retrieve your $users array, but if you can't, here would be the way to reformat it to fit your need :

$new_users = array();

foreach ($users as $user => $data) {
    $new_users[] = array(
        'name' => $user,
        'data' => array_values($data)
    );
}

echo json_encode($new_users);
share|improve this answer
    
@Shamrockonov explain 'almost'. I expect my answer might help –  user574632 Mar 19 at 13:05
    
This is almost exactly what I'm after, however, in the "data" array, I need to exclude the keys and just have the values. E.g {"name":"Gareth Cole","data":{"25732":180,"25689":2310,"25760":null,"25759":null,"25758":null,"2‌​5728":null,"25734":null}}" becomes {"name":"Gareth Cole","data":{180,:2310,null,null,null,null,null}}" –  Shamrockonov Mar 19 at 13:05
    
@Shamrockonov yup, thought so –  user574632 Mar 19 at 13:06
    
@Shamrockonov I edited my post, look at array_values –  Yellow Bird Mar 19 at 13:07
    
@YellowBird Spot on mate, Thank you very much! –  Shamrockonov Mar 19 at 13:23

Firstly you need to format your Array after encode it to JSON.

$result = array();
foreach($users as $k => $v) {
       $result[] = array(
         'name' => $k,
         'data' => $v
       );
}

And finally

echo json_encode($result);
share|improve this answer
    
Why are you using two loops ? –  Yellow Bird Mar 19 at 12:56
    
Because you use Array of Arrays –  wrivas Mar 19 at 12:58
    
And ? With foreach there is no need to loop through the first and second array. You can do it with only one loop. –  Yellow Bird Mar 19 at 12:59
    
Oh yes yes, Sorry :( –  wrivas Mar 19 at 13:02
    
No problem ;) was just sayin' –  Yellow Bird Mar 19 at 13:04

You can itterate the array and use array_values to get the format you require:

$fixed=array();
foreach ($users as $k => $v) {
    $fixed[]=array('name'=>$k, 'data'=>array_values($v));
}

echo json_encode($fixed);
share|improve this answer
    
This was what I was looking for also, Just didn't see your answer first. Thanks anyway. Much appreciated! –  Shamrockonov Mar 19 at 13:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.