This question already has an answer here:
这个问题在这里已有答案:
- Retrieve all parent keys of a given child key in Array 1 answer
- 在Array 1 answer中检索给定子键的所有父键
I have array which looks like this:
我有这样的数组:
$array = array(
53 => array(
'name' => 'category',
'subcats' => array(
42 => array(
'name' => 'subcategory',
),
152 => array(
'name' => 'subcategory',
'subcats' => array(
431 => array(
'name' => 'subsubcategory'
)
)
)
)
),
94 => array(
'name' => 'category'
),
12 => array(
'name' => 'category',
'subcats' => array(
67 => array(
'name' => 'subcategory',
),
237 => array(
'name' => 'subcategory',
),
109 => array(
'name' => 'subcategory',
),
32 => array(
'name' => 'subcategory',
)
)
);
As you can see keys are numbers, but those are IDs of categories which can be infinitely nested into each other. What I need is to search this array for specific ID and return array of it and its parents. We can safely assume that keys would not duplicate even in subcategories array - they are IDs from MySQL AI field;
正如您所看到的,键是数字,但这些是类别的ID,可以无限地嵌套在一起。我需要的是在这个数组中搜索特定的ID并返回它的数组及其父节点。我们可以放心地假设即使在子类别数组中键也不会重复 - 它们是来自MySQL AI字段的ID;
So the expected result for searching key 431 would be:
因此,搜索关键字431的预期结果将是:
array(53, 152, 431);
Any help would be appreciated, I just can't wrap my head around this ;)
任何帮助将不胜感激,我只是无法绕过这个;)
2 个解决方案
#1
1
This works -
这工作 -
function find_id_list($array, $key){
if(in_array($key,array_keys($array))){
return Array($key);
}
foreach($array as $k=>$v){
if(in_array("subcats",array_keys($v))){
$result = find_id_list($v["subcats"], $key);
if ($result !== null){
array_unshift($result,$k);
return $result;
}
}
}
return null;
}
OUTPUT:
OUTPUT:
$val = find_id_list($array, 431);
var_dump($val);
/* OUTPUT
array
0 => int 53
1 => int 152
2 => int 431
*/
$val = find_id_list($array, 109);
var_dump($val);
/* OUTPUT
array
0 => int 12
1 => int 109
*/
$val = find_id_list($array, 0);
var_dump($val);
/* OUTPUT
null
*/
#2
0
You can use a recursive function to solve this issue.
您可以使用递归函数来解决此问题。
function searchmulti($arr, $num)
{
if (!isset($arr[$num])) {
foreach ($arr as $key => $subarr)
{
if (isset($subarr['subcats']))
{
$ret = searchmulti($subarr['subcats'], $num);
if ($ret)
{
$ret[] = $key;
return $ret;
}
}
}
} else return array($num);
return null;
}
$solution = array_reverse(searchmulti($array,431));
Output
产量
$solution => array(53,152,431);
$ solution => array(53,152,431);
#1
1
This works -
这工作 -
function find_id_list($array, $key){
if(in_array($key,array_keys($array))){
return Array($key);
}
foreach($array as $k=>$v){
if(in_array("subcats",array_keys($v))){
$result = find_id_list($v["subcats"], $key);
if ($result !== null){
array_unshift($result,$k);
return $result;
}
}
}
return null;
}
OUTPUT:
OUTPUT:
$val = find_id_list($array, 431);
var_dump($val);
/* OUTPUT
array
0 => int 53
1 => int 152
2 => int 431
*/
$val = find_id_list($array, 109);
var_dump($val);
/* OUTPUT
array
0 => int 12
1 => int 109
*/
$val = find_id_list($array, 0);
var_dump($val);
/* OUTPUT
null
*/
#2
0
You can use a recursive function to solve this issue.
您可以使用递归函数来解决此问题。
function searchmulti($arr, $num)
{
if (!isset($arr[$num])) {
foreach ($arr as $key => $subarr)
{
if (isset($subarr['subcats']))
{
$ret = searchmulti($subarr['subcats'], $num);
if ($ret)
{
$ret[] = $key;
return $ret;
}
}
}
} else return array($num);
return null;
}
$solution = array_reverse(searchmulti($array,431));
Output
产量
$solution => array(53,152,431);
$ solution => array(53,152,431);