我有一堆名字-父名對,我把它們變成了一個層次結構的樹結構。例如,這些是我的配對:
Child : Parent
H : G
F : G
G : D
E : D
A : E
B : C
C : E
D : 0
使用此功能后發現here。
$array = array('H' => 'G', 'F' => 'G', ..., 'D' => null);
function to_tree($array)
{
$flat = array();
$tree = array();
foreach ($array as $child => $parent) {
if (!isset($flat[$child])) {
$flat[$child] = array();
}
if (!empty($parent)) {
$flat[$parent][$child] =& $flat[$child];
} else {
$tree[$child] =& $flat[$child];
}
}
return $tree;
}
$array_tree = to_tree($array)
我得到一個這樣的陣列:
Array
(
[7] => Array
(
[24] => Array
(
)
[38] => Array
(
)
[78] => Array
(
)
[103] => Array
(
)
[121] => Array
(
)
[163] => Array
(
[162] => Array
(
)
[213] => Array
(
)
[214] => Array
(
)
[215] => Array
(
)
)
...
我需要的是獲取我正在尋找的密鑰陣列以及所有子密鑰。假設我正在尋找密鑰 7,所以我會得到一個像這樣的陣列:
Array
(
[0] => 7
[1] => 24
[2] => 38
[3] => 78
[4] => 103
[5] => 121
[6] => 163
[7] => 162
[8] => 213
[9] => 214
[10] => 215
...
)
但我還需要查看像 163 這樣的鍵并獲得:
Array
(
[0] => 163
[1] => 162
[2] => 213
[3] => 214
[4] => 215
)
我認為對于有經驗的用戶來說并不難,但我不太明白。
uj5u.com熱心網友回復:
<?php
$data = array(
'H' => 'G',
'F' => 'G',
'G' => 'D',
'E' => 'D',
'A' => 'E',
'B' => 'C',
'C' => 'E',
'D' => '0'
);
$root_node = 'G'; // Change the root node here.
$parents_with_children = array();
// 1) First create a data structure with parents and children.
foreach($data as $child => $parent){
if(!isset($parents_with_children[$parent])){
$parents_with_children[$parent] = array();
}
$parents_with_children[$parent][] = $child;
}
// 2) Then call a recursive function to find the tree structure, from a specific root node
find_children($root_node, $parents_with_children);
function find_children($root_node, $parents_with_children){
echo '<br>Parent:'.$root_node;
$found_children = array();
foreach($parents_with_children as $parent => $children){
if($parent === $root_node){
foreach($children as $child){
if(!in_array($child, $found_children)){
echo '<br>- Child:'.$child;
$found_children[] = $child;
}
}
}
}
foreach($found_children as $child){
find_children($child, $parents_with_children);
}
} // find_children
?>
uj5u.com熱心網友回復:
經過大量挖掘后,我想出了這個解決方案:
function get_children($needle, array $haystack, array &$result = null, $found = false) {
// This is to initialize the result array and is only needed for
// the first call of this function
if (is_null($result)) {
$result = [];
}
foreach ($haystack as $key => $value) {
// Check whether the key is the value we are looking for. If the value
// is not an array, add it to the result array.
if ($key === $needle && empty($value)) {
//key found but has no children, so lets add the key to the result.
$result[] = $key;
}
elseif ($key === $needle && !empty($value)) {
// key found and it has children so let's find all the children.
$result[] = $key;
get_children(null, $value, $result, true);
}
elseif ($found && empty($value)) {
// If key found then we add the children.
$result[] = $key;
}
elseif ($found && !empty($value)) {
// If key found but children has more children then were looking for them.
$result[] = $key;
get_children(null, $value, $result, true);
}
elseif (!empty($value)) {
// If we haven't found the key, we keep looking through subarrays.
get_children($needle, $value, $result, false);
}
}
return $result;
}
這很好用,我不知道這是否是最好的解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515758.html
