我在PHP中遇到這個問題,當我嘗試將數組轉換爲json時 我有一個遞歸函數,它構建數組並將其編碼爲jSon格式。JSON問題需要幫助
這就是數組:
$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'),
);
這就是我的功能:
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;
}
那是我的JSON,但此JSON有錯誤:
{
"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": {}
}
}
琴絃「0」:和「5」:都是ca在json標記中使用錯誤。 我認爲嘗試使用preg_replace刪除它,但我很糟糕的正則表達式工作。有人可以幫助我吧...:D
謝謝你們!
我有一個想法... :)
我一直在尋找各種方法來測試變薄屎...:d 。我來這裏有一個想法,從插件和ENCODE得到JSON它作爲一個數組並打印出來,看看我需要給JSON Encode來給我正確的東西。
他們,他重現:
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
)
)
)
)
)
)
的JSON其上的對象的基礎上,我認爲它的存在的bug,IM tryng使用數組的工作,並將其轉換,當數據的對象。
有人知道如何創建類似的東西? 只是一個方式我能想到的...謝謝傢伙...
:DDD
而不是將整個東西轉換成JSON,使用一個循環,並將每個迭代轉換成JSON。 – Ibu
p.s.使用'foreach()'引用時要非常小心;看到這個評論的例子:http://www.php.net/manual/en/control-structures.foreach.php#101425 – ken
我會認爲刪除「1」和「5」會使它無效的JSON。爲什麼不按照你希望JSON數據結構來構建原始數據結構,而不是試圖將一個數據結構轉換成完全不同的數據結構? – dqhendricks