i'm with this problem at PHP when i'm try to convert an array to an json I have an recursive function that build the array to encode it on jSon format.
That's the array:
$data = array(
array('id' => 1, 'parent_id' => null, 'text' => 'lorem ipsum'),
array('id' => 2, 'parent_id' => 1, 'text' => 'lorem ipsum1'),
array('id' => 3, 'parent_id' => 1, 'text' => 'lorem ipsum2'),
array('id' => 4, 'parent_id' => 2, 'text' => 'lorem ipsum3'),
array('id' => 5, 'parent_id' => 3, 'text' => 'lorem ipsum4'),
array('id' => 6, 'parent_id' => null, 'text' => 'lorem ipsum5'),
);
Thats my function:
function trataArray($data) {
$itemsByReference = array();
// Build array of item references:
foreach ($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['id']]['data'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach ($data as $key => &$item)
if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;
// Remove items that were added to parents elsewhere:
foreach ($data as $key => &$item) {
if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($data[$key]);
}
// Encode:
return $data;
}
Thats my json, but this json have errors:
{
"0": // Problem here... T_T
{
"id": 1,
"parent_id": null,
"name": "lorem ipsum",
"children": [
{
"id": 2,
"parent_id": 1,
"name": "lorem ipsum1",
"children": [
{
"id": 4,
"parent_id": 2,
"name": "lorem ipsum3",
"children": [],
"data": {}
}
],
"data": {}
},
{
"id": 3,
"parent_id": 1,
"name": "lorem ipsum2",
"children": [
{
"id": 5,
"parent_id": 3,
"name": "lorem ipsum4",
"children": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
"5": // And here... T_T
{
"id": 6,
"parent_id": null,
"name": "lorem ipsum5",
"children": [],
"data": {}
}
}
The strings "0": and "5": are causing an error on json markup. i thinked to try to remove it using preg_replace but i'm terrible working with regular expressions. Someone could help me with it... :D
Thanks guys!
I had an idea... :)
I was looking for ways to test thins shit... :D And i come with an idea, get the JSON from the plugin and ENCODE it as one array and print it to see what i would need to give to JSON Encode to give me the correct thing.
Them he reproduce it:
Array
(
[0] => stdClass Object
(
[id] => 1
[text] => City
[children] => Array
(
[0] => stdClass Object
(
[id] => 11
[text] => Wyoming
[children] => Array
(
[0] => stdClass Object
(
[id] => 111
[text] => Albin
)
[1] => stdClass Object
(
[id] => 112
[text] => Canon
)
[2] => stdClass Object
(
[id] => 113
[text] => Egbert
)
)
)
[1] => stdClass Object
(
[id] => 12
[text] => Washington
[state] => closed
[children] => Array
(
[0] => stdClass Object
(
[id] => 121
[text] => Bellingham
)
[1] => stdClass Object
(
[id] => 122
[text] => Chehalis
)
[2] => stdClass Object
(
[id] => 123
[text] => Ellensburg
)
[3] => stdClass Object
(
[id] => 124
[text] => Monroe
)
)
)
)
)
)
The JSON its base on OBJECTS, i think the bug its there, im tryng to work with arrays and convert it, when the data its an object.
Someone knows how to create something like it? Just an way to i can think... Thanks guys...
:DDD