Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 339

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 392

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 394
PHP 多维数组的键值写入与读取 - Joyber 的博客


function arrayGet($array, $key, $default = null) {
    $keys = explode('.', $key);
    $a = $array;
    while (count($keys) != 0) {
        $key = array_shift($keys);
        if(!isset($a[$key])){
            return $default;
        }
        $a = $a[$key];
    }
    return $a;
}

function arraySet(&$array, $key, $value) {
    $keys = explode('.', $key);
    if (count($keys) == 1) {
        $array[$key] = $value;
        return;
    }
    $a = array();
    $b = $array;
    while (count($keys) != 0) {
        $k = array_shift($keys);
        $b = isset($b[$k]) ? $b[$k] : array();
        $a[$k] = $b;
    }
    $ka = array_keys($a);
    $a[end($ka)] = $value;
    for ($index = count($ka) - 2; $index >= 0; $index--) {
        $k = $ka[$index];
        $nextK = $ka[$index + 1];
        $a[$k] = array_merge($a[$k], array($nextK => $a[$nextK]));
    }
    $array[$ka[0]] = $a[$ka[0]];
}

标签: php

添加新评论