0

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?

4
  • 1
    Where do you get the first array from ? Commented Mar 19, 2014 at 12:48
  • You have to convert php array to the desired format befor encoding it to JSON Commented Mar 19, 2014 at 12:51
  • you'll have to iterate over your data and reformat a new array from it ... what else ? Commented Mar 19, 2014 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. Commented Mar 19, 2014 at 12:52

3 Answers 3

2

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

4 Comments

@Shamrockonov explain 'almost'. I expect my answer might help
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,"25728":null,"25734":null}}" becomes {"name":"Gareth Cole","data":{180,:2310,null,null,null,null,null}}"
@Shamrockonov yup, thought so
@Shamrockonov I edited my post, look at array_values
1

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);

1 Comment

This was what I was looking for also, Just didn't see your answer first. Thanks anyway. Much appreciated!
0

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);

4 Comments

Why are you using two loops ?
Because you use Array of Arrays
And ? With foreach there is no need to loop through the first and second array. You can do it with only one loop.
No problem ;) was just sayin'

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.