Код: Выделить всё
$a = array (
array('id'=>0, ....),
array('id'=>1, ....),
array('id'=>0, ....),
array('id'=>2, ....),
array('id'=>2, ....),
..........................
array('id'=>n, ....),
);
Код: Выделить всё
$a = array (
array('id'=>0, ....),
array('id'=>1, ....),
array('id'=>0, ....),
array('id'=>2, ....),
array('id'=>2, ....),
..........................
array('id'=>n, ....),
);
Код: Выделить всё
private function getByLvl ($arr, $id = 1) {
return array_filter($arr, function ($var) {
return ($var->id == $id);
});
}
Код: Выделить всё
Depth of a Sub-Tree
When we need depth information for a sub-tree, we cannot limit either the node or parent tables in our self-join because it will corrupt our results. Instead, we add a third self-join, along with a sub-query to determine the depth that will be the new starting point for our sub-tree:
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.name
ORDER BY node.lft;
+----------------------+-------+
| name | depth |
+----------------------+-------+
| PORTABLE ELECTRONICS | 0 |
| MP3 PLAYERS | 1 |
| FLASH | 2 |
| CD PLAYERS | 1 |
| 2 WAY RADIOS | 1 |
+----------------------+-------+
This function can be used with any node name, including the root node. The depth values are always relative to the named node.
Код: Выделить всё
Find the Immediate Subordinates of a Node
Imagine you are showing a category of electronics products on a retailer web site. When a user clicks on a category, you would want to show the products of that category, as well as list its immediate sub-categories, but not the entire tree of categories beneath it. For this, we need to show the node and its immediate sub-nodes, but no further down the tree. For example, when showing the PORTABLE ELECTRONICS category, we will want to show MP3 PLAYERS, CD PLAYERS, and 2 WAY RADIOS, but not FLASH.
This can be easily accomplished by adding a HAVING clause to our previous query:
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.name
HAVING depth <= 1
ORDER BY node.lft;
+----------------------+-------+
| name | depth |
+----------------------+-------+
| PORTABLE ELECTRONICS | 0 |
| MP3 PLAYERS | 1 |
| CD PLAYERS | 1 |
| 2 WAY RADIOS | 1 |
+----------------------+-------+
If you do not wish to show the parent node, change the HAVING depth <= 1 line to HAVING depth = 1 .
Код: Выделить всё
$a = array(
array('id' => 0,),
array('id' => 1),
array('id' => 0),
array('id' => 2),
array('id' => 2),
array('id' => 1));
//structure
$hash = array();
foreach ($a as $el)
{
$hash[$el['id']][] = $el;
}
ksort($hash);
var_dump($hash);
А цикл это какое то стороннее расширение и не является встроенным средством?Понятно что можно обойти циклом и посмотреть, я же спрашивал про встроенные средства в php:
Код: Выделить всё
class SuperFilter {
private $a;
private $b;
function __construct($a,$b)
{
$this->a = $a;
$this->b = $b;
}
private function doFilter($var)
{
return in_array($var['id'],$this->a);
}
public function result()
{
return array_filter($this->b,array($this,'doFilter'));
}
}
$a = array(1,2);
$b = array(
array('id' => 0,),
array('id' => 2),
array('id' => 0),
array('id' => 2),
array('id' => 2),
array('id' => 1));
$filter = new SuperFilter($a,$b);
var_dump($filter->result());
Код: Выделить всё
$a = array(1,2);
$b = array(
array('id' => 0,),
array('id' => 2),
array('id' => 0),
array('id' => 2),
array('id' => 2),
array('id' => 1));
$res = array();
foreach($b as $k=> $el)
{
if(in_array($el['id'],$a))
$res[$k] = $el;
}
var_dump($res);