![](/img/trans.png)
[英]How to get all children categories recursively by parent category in Laravel
[英]how to get the parent,and its children ,children category details using php
我想顯示一個;; 級別類別。
function fn_blog_tree_format(Array $data, $parent = 0) {
$tree = array();
foreach ($data as $d) {
if ($d['parentid'] == $parent) {
$children = fn_blog_tree_format($data, $d['id']);
if (!empty($children)) {
$d['_children'] = $children;
}
$tree[] = $d;
}
}
return $tree;
}
我使用上面的代碼來顯示所有類別級別。在這里,我得到以下輸出
Array
(
[0] => Array
(
[id] => 1
[categoryname] => php
[slugurl] => core-php
[parentid] => 0
[description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
[_children] => Array
(
[0] => Array
(
[id] => 2
[categoryname] => yii
[slugurl] => yii
[parentid] => 1
[description] => Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
[_children] => Array
(
[0] => Array
(
[id] => 4
[categoryname] => demo
[slugurl] => demo
[parentid] => 2
[description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsu
)
)
)
)
)
)
現在我想改變
[_children] => Array
(
[0] => Array
(
[id] => 4
[categoryname] => demo
[slugurl] => demo
[parentid] => 2
[description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsu
)
)
至
[_subchildren] => Array
(
[0] => Array
(
[id] => 4
[categoryname] => demo
[slugurl] => demo
[parentid] => 2
[description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsu
)
)
我該怎么辦。 請幫我。
我剛剛有一個簡短的主意(但沒有測試):您可以添加一個計數器。 首先將其設置為1,然后在循環中將其設置為1,然后再遞增。 當它為1時,您執行$d['_children'] = $children;
否則(當它大於1時)執行$d['_subchildren'] = $children;
Redmint為每個父母重置計數器
如果我對您的問題的理解正確,那么您要將所有孩子的孩子重命名為_subchildren
。 因此,我猜測可以通過檢查父變量的值來完成。
嘗試以此替換函數中的if條件塊-
if (!empty($children)) {
if($parent == 0){
$d['_children'] = $children;
}else{
$d['_subchildren'] = $children;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.