现在我有一个具有 parent_id 的数组 & 我想我需要使用递归来完成我想做的事情
array
0 =>
object(Menu)[56]
public 'attributes' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
1 =>
object(Menu)[60]
public 'attributes' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
2 =>
object(Menu)[61]
public 'attributes' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
array
0 =>
object(Menu)[56]
public 'attributes' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
1 =>
object(Menu)[60]
public 'attributes' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
2 =>
object(Menu)[61]
public 'attributes' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
array
0 =>
object(Menu)[56]
public 'attributes' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
1 =>
object(Menu)[60]
public 'attributes' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
2 =>
object(Menu)[61]
public 'attributes' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
array
0 =>
object(Menu)[56]
public 'attributes' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '1' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'home' (length=4)
'menu_url' => string 'test' (length=4)
'parent_id' => string '0' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
1 =>
object(Menu)[60]
public 'attributes' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '2' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Contact Us' (length=10)
'menu_url' => string 'contact' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
2 =>
object(Menu)[61]
public 'attributes' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'original' =>
array
'id' => string '3' (length=1)
'menu_id' => string '1' (length=1)
'menu_name' => string 'Privacy Docs' (length=12)
'menu_url' => string 'privacy' (length=7)
'parent_id' => string '1' (length=1)
'position' => string '0' (length=1)
'group_id' => string '1' (length=1)
'menu_items' => null
public 'relationships' =>
array
empty
public 'exists' => boolean true
public 'includes' =>
array
empty
现在我在一个名为 menuHelper 的类中有一个函数
public static function make(array $array, $no = 0) {
var_dump($array);
$child = self::hasChildren($array, $no);
if (empty($child))
return "";
$content = "<li>\n";
foreach ($child as $value) {
$content = sprintf("\t<li><div>%s</div>" . self::make($array, $value->id) . "</li>\n", $value->menu_name);
//$content .= self::make($array, $value->id);
}
$content .= "</li>\n";
//return $content;
return($content == '' ? '' : "<ol>" . $content . "</ol>");
}
public static function hasChildren($array, $id) {
return array_filter($array, function ($var) use($id) {
return $var->parent_id == $id;
});
}
我希望这个输出这个
<ol class="sortable">
<li>
<div>parent</div>
<ol>
<div>Sub</div>
<ol>
<div>Sub 2</div>
</ol>
</ol>
</li>
<li>
<div>parent 2</div>
<ol>
<div>Sub</div>
<ol>
<div>Sub 2</div>
</ol>
</ol>
</li>
</ol>
但这并没有继续输出父母,而是输出 1 个父母,然后是它的孩子。然后即使有另一个父母它也会退出输出所以我想我有什么问题吗?