3

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.

4
  • Please post the actual JSON. Your brackets aren't balanced. Commented Nov 15, 2014 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? Commented Nov 15, 2014 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! Commented Nov 15, 2014 at 0:44
  • The original output also has mismatched brackets. There's no opening [ for the closing ] above added:. Commented Nov 15, 2014 at 0:46

3 Answers 3

3

Change the $dates assignment to:

$dates = array(array('eu' => '59.99'), array('us' => '39.99'));
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for your quick reply and help with this :)
0

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 }

Comments

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

Comments

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.