我有这个多维数组:
[0] => Array (
[id] => 1
[name] => Ropa
[slug] => ropa
[lft] => 1
[lvl] => 0
[rgt] => 2
[__children] => Array ( )
)
[1] => Array (
[id] => 3
[name] => Calzado
[slug] => calzado
[lft] => 1
[lvl] => 0
[rgt] => 8
[__children] => Array (
[0] => Array (
[id] => 10
[name] => Zapatos y botas
[slug] => zapatos-y-botas
[lft] => 2
[lvl] => 1
[rgt] => 3
[__children] => Array ( )
)
[1] => Array (
[id] => 11
[name] => Zapatillas
[slug] => zapatillas
[lft] => 4
[lvl] => 1
[rgt] => 7
[__children] => Array (
[0] => Array (
[id] => 14
[name] => Zapatillas rojas
[slug] => zapatillas-rojas
[lft] => 5
[lvl] => 2
[rgt] => 6
[__children] => Array ( )
)
)
)
)
)
[2] => Array (
[id] => 13
[name] => Accesorios
[slug] => accesorios
[lft] => 1
[lvl] => 0
[rgt] => 2
[__children] => Array ( )
)
我可以使用以下方法过滤数组的第一级节点:
$foobar = array_filter($navbar, function($value) {
return $value['slug'] == 'accesorios' || $value['slug'] == 'ropa';
});
所以我明白了:
array (size=2)
0 =>
array (size=7)
'id' => int 1
'name' => string 'Ropa' (length=4)
'slug' => string 'ropa' (length=4)
'lft' => int 1
'lvl' => int 0
'rgt' => int 2
'__children' =>
array (size=0)
empty
2 =>
array (size=7)
'id' => int 13
'name' => string 'Accesorios' (length=10)
'slug' => string 'accesorios' (length=10)
'lft' => int 1
'lvl' => int 0
'rgt' => int 2
'__children' =>
array (size=0)
empty
但是当我尝试使用它来过滤第二级或第三级节点时,例如:
$foobar = array_filter($navbar, function($value) {
return $value['slug'] == 'zapatillas';
});
我得到空结果..那么如何在第二个或第三个数组中过滤?
答案 0 :(得分:1)
如果要返回包含该值的完整数组,可以使用以下命令:
$foobar = array_filter($navbar, function($value) {
//return true if child slug key contains zapatillas as a value
return in_array('zapatillas', array_column($value['__children'], 'slug'));
});
答案 1 :(得分:-1)
可能会有所帮助并且与您的问题有关。据我所知,这是你失踪的递归吗? https://stackoverflow.com/a/6795671/2660253
答案 2 :(得分:-2)
$foobar = array_filter($navbar, function($value) { foreach($value as $val) {
return $val['slug'] == 'zapatillas'; }
});