排序陣列我有一個返回SQL查詢:由家長/遞歸
Array
(
[0] => stdClass Object
(
[id] => 1
[parent] =>
[created_at] => 2015
[updated_at] => 0
[name] => Strona Główna
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[1] => stdClass Object
(
[id] => 2
[parent] => 1
[created_at] => 2015
[updated_at] => 0
[name] => Podstrona strony głównej
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[2] => stdClass Object
(
[id] => 3
[parent] =>
[created_at] => 2015
[updated_at] => 2015
[name] => O nas
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[3] => stdClass Object
(
[id] => 5
[parent] => 1
[created_at] => 2015
[updated_at] => 2015
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[4] => stdClass Object
(
[id] => 6
[parent] => 3
[created_at] => 2015
[updated_at] => 0
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[5] => stdClass Object
(
[id] => 8
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => tuytuytuyt
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
[6] => stdClass Object
(
[id] => 9
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => fghfhgfh
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
)
)
而且我幾乎成功到達排序。如果元素有一個父元素,它會創建一個數組並將子元素添加到此元素,但是最後一個元素存在問題,我不知道爲什麼。
現在看起來就像這樣:
Array
(
[0] => stdClass Object
(
[id] => 1
[parent] =>
[created_at] => 2015
[updated_at] => 0
[name] => Strona Główna
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
[0] => stdClass Object
(
[id] => 2
[parent] => 1
[created_at] => 2015
[updated_at] => 0
[name] => Podstrona strony głównej
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[1] => stdClass Object
(
[id] => 5
[parent] => 1
[created_at] => 2015
[updated_at] => 2015
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
[0] => stdClass Object
(
[id] => 8
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => tuytuytuyt
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[1] => stdClass Object
(
[id] => 9
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => fghfhgfh
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
)
)
)
)
[1] => stdClass Object
(
[id] => 2
[parent] => 1
[created_at] => 2015
[updated_at] => 0
[name] => Podstrona strony głównej
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[2] => stdClass Object
(
[id] => 3
[parent] =>
[created_at] => 2015
[updated_at] => 2015
[name] => O nas
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
[0] => stdClass Object
(
[id] => 6
[parent] => 3
[created_at] => 2015
[updated_at] => 0
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
)
)
[3] => stdClass Object
(
[id] => 5
[parent] => 1
[created_at] => 2015
[updated_at] => 2015
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
[0] => stdClass Object
(
[id] => 8
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => tuytuytuyt
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[1] => stdClass Object
(
[id] => 9
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => fghfhgfh
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
)
)
[4] => stdClass Object
(
[id] => 6
[parent] => 3
[created_at] => 2015
[updated_at] => 0
[name] => Kolejna podstrona
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[5] => stdClass Object
(
[id] => 8
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => tuytuytuyt
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
[6] => stdClass Object
(
[id] => 9
[parent] => 5
[created_at] => 2015
[updated_at] => 0
[name] => fghfhgfh
[short] =>
[content] =>
[header] =>
[img] =>
[visible] => 0
[position] => 1000
[top] => 0
[left] => 0
[footer] => 0
[dropdown] => 0
[chilldren] => 0
[title] =>
[description] =>
[keywords] =>
[tags] =>
[redirect] =>
[js] =>
[css] =>
[css_class] =>
[module] =>
[lang] => 0
[node] => Array
(
)
)
)
正如你所看到的最後的元素進行過濾。
守則「排序」父母的模樣:
$new = [];
foreach($data as $key => $item) {
$data[$key] - > node = [];
array_push($new, $data[$key]);
for ($i = 0; $i < count($new); $i++) {
if ($new[$i] - > id == $data[$key] - > parent) {
array_push($new[$i] - > node, $data[$key]);
}
}
}
print_r($data);
誰能給我解釋一下我做錯了?
輸出中最後一個元素有什麼問題? ID爲6,8和9的節點沒有子節點,所以它們單獨出現,我會發現它是正常的。你可以向你的問題添加你想要的輸出嗎? – trincot
他們屬於特定的「父母」,因爲你可以看到我不想和我不工作與「兒童」的關鍵。 – Mehow