PHP 地址数组树节点使用字符串或数组


PHP address array tree node using string or array

比如说,我在树结构中有数据,实现为任意深度的数组数组,类似于

print_r($my_array);
Array
(
    [id] => 123
    [value] => Hello, World!
    [child] => Array
        (
            [name] => Foo
            [bar] => baz
        )
    [otherchild] => Array
        (
            [status] => fubar
            [list] => Array
                (
                    [one] => 1
                    [two] => 3
                )
        )
    [sanity] => unchecked
)

现在,使用单个字符串作为键,我想在任意深度寻址节点,假设我有一个键,如下所示:

$key = 'otherchild|list|two';

使用此键,我希望能够解决存储在

$my_array['otherchild']['list']['two']

显然,我可以爆炸('|',$key)来获取一个键数组,并且从中转移值并使用这些值来寻址子数组可以轻松获得我正在寻找的值,例如

$value = $my_array;
$keys = explode('|', $key);
while ($k = array_shift($keys)) {
    if (isset($value[$k])) {
        $value = $value[$k];
    } else {
        // handle failure
    }
} // Here, if all keys exist, $value holds value of addressed node

但是我被困在尝试以通用方式更新值的困境中,即不必求助于类似的东西

$keys = explode('|', $key);
if (count($keys) == 1) {
    $my_array[$keys[0]] = $new_value;
} else if (count($keys) == 2) {
    $my_array[$keys[0]][$keys[1]] = $new_value;
} else if ...

有什么想法吗?

function setAt(array & $a, $key, $value)
{
    $keys = explode('|', $key);
    // Start with the root node (the array itself)
    $node = & $a;
    // Walk the tree, create nodes as needed
    foreach ($keys as $k) {
        // Create node if it does not exist
        if (! isset($node[$k])) {
             $node[$k] = array();
        }
        // Walk to the node
        $node = & $node[$k];
    }
    // Position found; store the value
    $node = $value;
}

// Test
$array = array();
// Add new values
setAt($array, 'some|key', 'value1');
setAt($array, 'some|otherkey', 'val2');
setAt($array, 'key3', 'value3');
print_r($array);
// Overwrite existing values
setAt($array, 'some|key', 'new-value');
print_r($array);
setAt($array, 'some', 'thing');
print_r($array);

如果你正在寻找一个简短的答案,你也可以使用 eval()。

$elem = "'$array['" . str_replace("|", "']['", $key) . "']";
$val = eval("return isset($elem) ? $elem : null;");