Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys... ive been cracking my brains out trying to make this line of code to be in this format

{'title': 'Item 1'},
{'title': 'Folder 2', 'isFolder': true, 'key': 'folder2',
   'children': {
      'title': 'Sub-item 2.1'
    },{
      'title': 'Sub-item 2.2'
    }
},
{'title': 'Folder 3', 'isFolder': true, 'key': 'folder3',
   'children': {
       'title': 'Sub-item 3.1'
    },{
       'title': 'Sub-item 3.2'
    }
 },
{'title': 'Item 5'}


so basically i want this to be return in php what i mean is what will it look like in a php format before converting it using json_encode?

i was thinking of something like this
<?php 
 $array= array(
    'title' => 'folder1',
  /* i dont know what comes next... please help guys... */
 );

?>
Posted

1 solution

Hi
I guess it's best to show some example code
PHP
$a = array();
echo json_encode($a), "\n";
echo json_encode($a, JSON_FORCE_OBJECT), "\n\n";

$a = array(1,2,3,4,5,6,7);
echo json_encode($a), "\n";
echo json_encode($a, JSON_FORCE_OBJECT), "\n\n";

$a = array(array(1,2,3),array(5,6,7));
echo json_encode($a), "\n";
echo json_encode($a, JSON_FORCE_OBJECT), "\n\n";

$a = array(array("firstname" => "John","lastname" => "Doe","age"=>42 ),array("firstname" => "Jane","lastname" => "Doe","age"=>42 ));
echo json_encode($a), "\n";
echo json_encode($a, JSON_FORCE_OBJECT), "\n\n";
?>

This code results in:
[]
{}

[1,2,3,4,5,6,7]
{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7}

[[1,2,3],[5,6,7]]
{"0":{"0":1,"1":2,"2":3},"1":{"0":5,"1":6,"2":7}}

[{"firstname":"John","lastname":"Doe","age":42},{"firstname":"Jane","lastname":"Doe","age":42}]
{"0":{"firstname":"John","lastname":"Doe","age":42},"1":{"firstname":"Jane","lastname":"Doe","age":42}}


Hope this helps

regards
Michel
 
Share this answer
 
Comments
Madzmar25 12-Feb-12 20:06pm    
yes that's what i was looking for... thanks again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900