我的桌子结构如下所示,其中item_id -> 1,2是主要的菜单项.item_id ->3,4是1的子项,可以由parent_id确定。

我正在使用下面的代码来获取和迭代这个结果。并在UI上正确地构造菜单。
直到我使用了foreach循环“foreach-1”,一切都很好。结果是
1-包().2-附件().
但是当我加上foreach循环“foreach-2”时。结果没有如下所示(2-附件)
1-袋子(3,4,).
正如您可能看到的,我正在使用yalinqo库从结果集中获取数据。在万维网上,yalinqo的帮助要少得多。
是不是因为有什么隐藏的诅咒造成了问题?或者yalinqo查询的形成方式有什么问题吗?
如果你在yalinqo工作过,请在这里帮助我。因为我更多的是一个.net用户,并且对php的体验非常少。
我只是在这里张贴部分代码:
use \YaLinqo\Enumerable as E;
...
if ($connection)
{
$result = mysqli_query($connection, "CALL fetch_main_menu") or die("Query fail: " . mysqli_error());
$result_data = E::from ($result) ;
$top_menu = E::from ($result_data )
->where (function($item){return $item["parent_id"]==null;})
->select (function ($item){return $item;});
//foreach - 1
foreach ($top_menu as $i )
{
$item_id = $i["id"];
$item_name = $i["name"];
echo $item_id . '-' . $item_name ;
//fetch subitems
$sub_menu = E::from ($result_data)
->where (function($item){global $item_id; return $item["parent_id"] != null && $item["parent_id"] == $item_id;})
->select (function ($item){return $item;});
echo '(';
//foreach - 2
foreach($sub_menu as $sub_i)
{
echo $sub_i["id"] . ',';
}
unset($sub_menu);
echo ') ... ';
}
}P.S.:如果有菜单栏结构的话,我很乐意使用另一种更有效的方法。
发布于 2016-04-09 23:23:20
如果您对YaLinqo有任何问题,请不要犹豫直接问我,或者在YaLinqo问题追踪器中创建一个支持问题。
您的代码在不必要的地方更复杂,在必要时不够复杂。:)
select (function ($item){return $item;})是不必要的,它什么也不做,只是where就足够了(如果您使用函数式语法,.NET LINQ也是如此):
$top_menu = E::from ($result_data) ->where (函数($item){返回$item"parent_id"==null;});global不同,您可以将use语句添加到匿名函数中:
$sub_menu = E::from ($result_data) ->where ($item) use($item_id) {返回$item"parent_id“!= null & $item"parent_id”== $item_id;};
与.NET不同,需要显式捕获变量。另外,在新代码中应该避免使用global。假设我们已经有了一个数组:
$items = [
[ 'id' => 1, 'name' => "BAGS", 'parent_id' => null ],
[ 'id' => 2, 'name' => "ACCESSORIES", 'parent_id' => null ],
[ 'id' => 3, 'name' => "Messenger", 'parent_id' => 1 ],
[ 'id' => 4, 'name' => "Sling", 'parent_id' => 1 ],
[ 'id' => 5, 'name' => "Earrings", 'parent_id' => 2 ],
[ 'id' => 6, 'name' => "Clip", 'parent_id' => 2 ],
];构建菜单层次结构可以这样做:
$menu = from($items)
->where(function ($ti) {
return $ti['parent_id'] === null;
})
->select(function ($ti) use ($items) {
return [
'menu' => $ti,
'items' => from($items)
->where(function ($si) use ($ti) {
return $si['parent_id'] === $ti['id'];
})
];
});
print_r($menu->toArrayDeep());输出:
Array
(
[0] => Array
(
[menu] => Array
(
[id] => 1
[name] => BAGS
[parent_id] =>
)
[items] => Array
(
[2] => Array
(
[id] => 3
[name] => Messenger
[parent_id] => 1
)
[3] => Array
(
[id] => 4
[name] => Sling
[parent_id] => 1
)
)
)
[1] => Array
(
[menu] => Array
(
[id] => 2
[name] => ACCESSORIES
[parent_id] =>
)
[items] => Array
(
[4] => Array
(
[id] => 5
[name] => Earrings
[parent_id] => 2
)
[5] => Array
(
[id] => 6
[name] => Clip
[parent_id] => 2
)
)
)
)或者,如果您只需要一个字符串:
$menustring = from($items)
->where(function ($ti) {
return $ti['parent_id'] === null;
})
->select(function ($ti) use ($items) {
return "{$ti['id']}-{$ti['name']}("
. from($items)
->where(function ($si) use ($ti) {
return $si['parent_id'] === $ti['id'];
})
->toString(',', function ($si) {
return "{$si['id']}-{$si['name']}";
})
. ")";
})
->toString(';');
echo($menustring);输出:
1-BAGS(3-Messenger,4-Sling);2-ACCESSORIES(5-Earrings,6-Clip)最后,一个递归函数,它适用于任意级别的嵌套:
function get_menu_with_subitems ($items, $item)
{
$subitems = from($items)
->where(function ($i) use ($item) {
return $i['parent_id'] === $item['id'];
})
->select(function ($i) use ($items) {
return get_menu_with_subitems($items, $i);
})
->toList();
return [
'id' => $item['id'],
'name' => $item['name'],
'items' => count($subitems) > 0 ? $subitems : null,
];
}
$root = [ 'id' => null, 'name' => 'Root' ];
$menu = get_menu_with_subitems($items, $root)['items'];
print_r($menu);输出:
(
[0] => Array
(
[id] => 1
[name] => BAGS
[items] => Array
(
[0] => Array
(
[id] => 3
[name] => Messenger
[items] =>
)
[1] => Array
(
[id] => 4
[name] => Sling
[items] =>
)
)
)
[1] => Array
(
[id] => 2
[name] => ACCESSORIES
[items] => Array
(
[0] => Array
(
[id] => 5
[name] => Earrings
[items] =>
)
[1] => Array
(
[id] => 6
[name] => Clip
[items] =>
)
)
)
)https://stackoverflow.com/questions/36270696
复制相似问题