Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to get my JSON output in a particular syntax

here is my code:

$ss = array('1.jpg', '2.jpg');
$dates = array('eu' => '59.99', 'us' => '39.99');
$array1 = array('name' => 'game1', 'publisher' => 'ubisoft', 'screenshots' => $ss, 'dates' => $dates, 'added' => '2014/12/31');

echo json_encode($array1);

it gives me this output:

{
name: "game1",
publisher: "ubisoft",
screenshots: [
"1.jpg",
"2.jpg"
],
dates: {
eu: "59.99",
us: "39.99"
},
],
added: "2014/12/31"
}

which is CLOSE but, not exactly what I need. The dates need to be formatted slightly different. Like this:

{
name: "game1",
publisher: "ubisoft",
screenshots: [
"1.jpg",
"2.jpg"
],
dates: [
{
eu: "59.99"
},
{
us: "39.99"
}
],
added: "2014/12/31"
}

I have tried adding more dimensions to the $dates array, but that still doesn't give quite the right output. Unfortunately the php manual for the json_encode() function doesn't provide much help and there is very little documentation on more complex json encodes within php that i have found with google searching.

Any help would be greatly appreciated.

share|improve this question
    
Please post the actual JSON. Your brackets aren't balanced. – Barmar Nov 15 '14 at 0:33
1  
Why do you want objects with different keys in the dates: array? Won't that make it difficult to access the values, since you don't know the key? – Barmar Nov 15 '14 at 0:35
    
@Barmar - Thanks for your quick reply. Sorry, I have edited the desired output. It was hand typed and I got it wrong. Also, a friend of mine who works with Json has given me a very specific specification of how he wants the Json to read. I know almost nothing about Json or why he needs it the way he does, so I'm actually quite surprised I've even made it this far! – Lee Ginger-Ninja McCarthy Nov 15 '14 at 0:44
    
The original output also has mismatched brackets. There's no opening [ for the closing ] above added:. – Barmar Nov 15 '14 at 0:46
up vote 1 down vote accepted

Change the $dates assignment to:

$dates = array(array('eu' => '59.99'), array('us' => '39.99'));
share|improve this answer
    
Many thanks for your quick reply and help with this :) – Lee Ginger-Ninja McCarthy Nov 15 '14 at 0:58

you will need to do something like this ( wrap each date in its own assoc array)

array( 
   array('eu' => '59.99'),
   array('us' => '39.99')
);

Then it will encode correctly as

[
   {'eu' : '59.99'},
   {'us' : '39.99'}
]

It's simple really, arrays with numeric( natural ) keys are just [ ], associative arrays are { key : value }

share|improve this answer
$ss = array('1.jpg', '2.jpg');
$dates[] = array('eu' => '59.99');
$dates[]=array('us' => '39.99');
$array1 = array('name' => 'game1', 'publisher' => 'ubisoft', 'screenshots' => $ss, 'dates' => $dates, 'added' => '2014/12/31');
echo json_encode($array1);
share|improve this answer

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.