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/>
Kline
  • 47
  • 7

3 Answers3

6

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!

acupofjose
  • 2,159
  • 1
  • 22
  • 40
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';
Dave
  • 867
  • 6
  • 11
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);
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15