0

I need to create a JSON object using PHP, as I need to give attributes to each node like XML uses I can't just create a load of PHP arrays (I think), so I am creating PHP objects and doing that way.

The problem is I can quite get the JSON formatted properly.

This is what I am trying:

$object = new stdClass();

$object->{'0'}['title'] = 'Home';
$object->{'0'}['entry'] = '123';

$object->{'1'}['title'] = 'About';
$object->{'1'}['entry'] = '123';

$object->{'2'}['title'] = 'Gallery';
$object->{'2'}['entry'] = '123';

$object->{'2'} = new stdClass();

$object->{'2'}->{'0'}['title'] = 'Past';
$object->{'2'}->{'0'}['entry'] = '1234';

$object->{'2'}->{'1'}['title'] = 'Present';
$object->{'2'}->{'1'}['entry'] = '1235';

$object->{'2'}->{'0'} = new stdClass();

$object->{'2'}->{'0'}->{'0'}['title'] = '1989';
$object->{'2'}->{'0'}->{'0'}['entry'] = '12345';

$object->{'2'}->{'0'}->{'1'}['title'] = '1990';
$object->{'2'}->{'0'}->{'1'}['entry'] = '12346';


$ob=json_encode($object);

echo $ob;

Which outputs:

{
"0":{"title":"Home","entry":"123"},
"1":{"title":"About","entry":"123"},
"2":{
"0":{
"0":{"title":"1989","entry":"12345"},
"1":{"title":"1990","entry":"12346"}},
"1":{"title":"Present","entry":"1235"}
}
} 

I need "2" of the first node to have attributes "title":"Gallery","entry":"123" but also contain the sub nodes for Past and Present, with the same again for the years.

In XML it may look something like this:

<0 title="Home" entry="123">
<0/>
<1 title="About" entry="123">
<1/>
<2 title="Gallery" entry="123">
  <0 title="Past" entry="1234">
     <0 title="1989" entry="12345"><0/>
     <1 title="1990" entry="12346"><1/>
  <0/>
  <1 title="Present" entry="1235">
  <1/>
<2/>
3

3 Answers 3

5

The easiest way to use json with PHP is to use the built in json_encode() and json_decode() functions.

This is really nice because you can encode php arrays straight into json without having to do anything!

$array = array(
    array(
        "title" => "Home",
        "entry" => "123" 
    ),
    array(
        "title" => "About",
        "entry" => "123" 
    ),
    array(
        "title" => "Gallery",
        "entry" => "123",
    ),
);

And continue to nest as such, you can then convert that into a json object:

$json = json_encode($array);

With an output like:

[{"title":"Home","entry":"123"},{"title":"About","entry":"123"},{"title":"Gallery","entry":"123"}]

You can then access these again with php by doing a json_decode and moving around it like an object.

I made a playground for you to mess with here: http://codepad.viper-7.com/qzMJO3

Hope that helps!

0
1

you're deleting them with your object creation:

swap these lines around:

$object->{'2'}['title'] = 'Gallery';
$object->{'2'}['entry'] = '123';
//this line creating the new object is effectively erasing the previous 2 lines.
$object->{'2'} = new stdClass();

to become:

$object->{'2'} = new stdClass();

$object->{'2'}['title'] = 'Gallery';
$object->{'2'}['entry'] = '123';
2
  • post the json of how you want it to look.
    – Dave
    Oct 27, 2014 at 16:25
  • Not sure how it would look I have added the XML equivalent
    – Kline
    Oct 27, 2014 at 16:35
0

You're setting $object->{'2'} and $object->{'2'}->{'0'} with new stdClass(), loosing the data you've set previously.

Try this:

<?php
$object = new stdClass();

$object->{'0'}['title'] = 'Home';
$object->{'0'}['entry'] = '123';

$object->{'1'}['title'] = 'About';
$object->{'1'}['entry'] = '123';

$object->{'2'}['title'] = 'Gallery';
$object->{'2'}['entry'] = '123';

$object->{'2'}['0']['title'] = 'Past';
$object->{'2'}['0']['entry'] = '1234';

$object->{'2'}['1']['title'] = 'Present';
$object->{'2'}['1']['entry'] = '1235';

$object->{'2'}['0']['0']['title'] = '1989';
$object->{'2'}['0']['0']['entry'] = '12345';

$object->{'2'}['0']['1']['title'] = '1990';
$object->{'2'}['0']['1']['entry'] = '12346';


$ob=json_encode($object);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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