我有一个类似
的数组 Array
(
[0] => Array
(
[id] => 81
[cata_key] => 908cbbcb86a1cf64b67c96ff
[cata_name] => Lunch
[app_key] => 2fabc0d9447c6375657dead4
[parentid] => 0
[subcategories] => Array
(
)
)
[1] => Array
(
[id] => 80
[cata_key] => baac98b4e73c05ebbf45bdc6
[cata_name] => Break Fast
[app_key] => 2fabc0d9447c6375657dead4
[parentid] => 0
[subcategories] => Array
(
[0] => Array
(
[id] => 82
[cata_key] => 5970b7afc450ef3b24573de9
[cata_name] => Rise Products
[app_key] => 2fabc0d9447c6375657dead4
[parentid] => 80
[subcategories] => Array
(
[0] => Array
(
[id] => 83
[cata_key] => 82e36d8f821e14fc8db1d4da
[cata_name] => Dosha
[app_key] => 2fabc0d9447c6375657dead4
[parentid] => 82
[subcategories] => Array
(
[0] => Array
(
[id] => 84
[cata_key] => 49730020d850439dd7de8747
[cata_name] => Masala Dosha
[app_key] => 2fabc0d9447c6375657dead4
[parentid] => 83
[subcategories] => Array
(
)
)
)
)
)
)
)
)
)
我想从特定的内部数组id中找到根索引子数组的深度。例如,我想从parentid = 0中找到根索引数组的深度,它应该是1.来自parentId = 0/80/82/83的根索引子数组的深度应为4.
实际上我的目标是限制阵列的深度。应该有一个深度限制。因此,当任何人尝试将子数组添加到此数组时,我们应该找出当前的数组深度。例如,数组深度限制为4,并且任何一个尝试在cata_name = Masala Dosha下添加子数组,已经达到的深度并且不应该允许添加新的子数组。但同时他可以在[cata_name] =>下添加一个子数组。午餐,因为它的深度只有1,深度限制是4。
答案 0 :(得分:0)
尝试这个,它有效,但我不喜欢这个
http://sandbox.onlinephpfunctions.com/code/9922bf9abec89477e5bd3a81c41feb953db604a1
$a=array(
array('id'=>81),
array('id'=>80,'subcategories'=>array('id'=>82,'subcategories'=>array('id'=>83,'subcategories'=>array('id'=>84,'subcategories'=>array())))),
);
echo "80:" . test($a,80);
echo PHP_EOL;
echo "81:" . test($a,81);
echo PHP_EOL;
echo "82:" . test($a,82);
echo PHP_EOL;
echo "83:" . test($a,83);
function test($a,$key){
$aa=array();
foreach($a as $v){
$b=array();
$content=print_r($v,true);
$b['content']=$content;
if(preg_match('/ {28}\[/',$content)){
$b['over']=1;
}else{
$b['over']=0;
}
$aa[]=$b;
}
foreach($aa as $v){
if(preg_match('/\[id\] => '.$key.'/',$v['content'])){
return $v['over'];
}
}
return 0;
}
然后
80:1
81:0
82:1
83:1
答案 1 :(得分:0)
我看到你接受了答案,但这里有另一种选择:
<?php
$arrayHandler = new ArrayHandler();
$arr = array();
//Adding array examples (without checking)
$arr[0]['subcategories'] = array('id'=>80);
$arr[0]['subcategories']['whatever'] = array('id'=>82);
$arr[0]['subcategories']['whatever']['whatever2'] = array('id'=>83);
//Will be added because this is fourth level from $arr[0]
$chk = $arrayHandler->addSubArray($arr[0], $arr[0]['subcategories']['whatever']['whatever2']['whatever3']);
if ($chk !== false) {
$arr[0]['subcategories']['whatever']['whatever2']['whatever3'] = array('id'=>84);
}
//Won't be added because this is fitfth level from $arr[0]
$chk = $arrayHandler->addSubArray($arr[0], $arr[0]['subcategories']['whatever']['whatever2']['whatever3']['whatever4']);
if ($chk !== false) {
$arr[0]['subcategories']['whatever']['whatever2']['whatever3']['whatever4'] = array('id'=>85);
}
echo print_r($arr,true);
class ArrayHandler {
private $depthLimit = 4;
private $depth = 1;
public function addSubArray($checkFromNode, $addNode) {
if (is_array($checkFromNode)) {
//Do recursion as long as node is an array and count depth
//If not an array anymore return value sent to this parameter
foreach ($checkFromNode as $key=>$node) {
if (is_array($node)) {
$this->depth++;
$this->addSubArray($node, $addNode);
}
}
if (intval($this->depth) > intval($this->depthLimit)) {
return false;
}
return true;
}
}
}
?>
输出将是示例:
Array ( [0] => Array ( [subcategories] => Array ( [id] => 80 [whatever] => Array ( [id] => 82 [whatever2] => Array ( [id] => 83 [whatever3] => Array ( [id] => 84 ) ) ) ) ) )