将分隔字符串转换为数组键路径并赋值

发布于 2024-12-06 01:10:12 字数 339 浏览 0 评论 0原文

我有一个像这样的字符串:

$string = 'one/two/third/four';

我将其转换为数组:

$keys =explode('/', $string) ;

该数组可以有任意数量的元素,例如 1、2、5 等。

如何为多维数组分配某个值,但使用上面创建的 $keys确定插入的位置?

就像:

$arr['one']['two']['third']['four'] = 'value';

抱歉,如果问题令人困惑,但我不知道如何为了更好地解释它

I have a string like this:

$string = 'one/two/three/four';

which I turn it into a array:

$keys = explode('/', $string);

This array can have any number of elements, like 1, 2, 5 etc.

How can I assign a certain value to a multidimensional array, but use the $keys I created above to identify the position where to insert?

Like:

$arr['one']['two']['three']['four'] = 'value';

Sorry if the question is confusing, but I don't know how to explain it better

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绿光 2024-12-13 01:10:12

这有点不简单,因为你想要嵌套,但它应该是这样的:

function insert_using_keys($arr, $keys, $value){
    // we're modifying a copy of $arr, but here
    // we obtain a reference to it. we move the
    // reference in order to set the values.
    $a = &$arr;

    while( count($keys) > 0 ){
        // get next first key
        $k = array_shift($keys);

        // if $a isn't an array already, make it one
        if(!is_array($a)){
            $a = array();
        }

        // move the reference deeper
        $a = &$a[$k];
    }
    $a = $value;

    // return a copy of $arr with the value set
    return $arr;
}

This is kind of non-trivial because you want to nest, but it should go something like:

function insert_using_keys($arr, $keys, $value){
    // we're modifying a copy of $arr, but here
    // we obtain a reference to it. we move the
    // reference in order to set the values.
    $a = &$arr;

    while( count($keys) > 0 ){
        // get next first key
        $k = array_shift($keys);

        // if $a isn't an array already, make it one
        if(!is_array($a)){
            $a = array();
        }

        // move the reference deeper
        $a = &$a[$k];
    }
    $a = $value;

    // return a copy of $arr with the value set
    return $arr;
}
深海里的那抹蓝 2024-12-13 01:10:12
$string = 'one/two/three/four';
$keys = explode('/', $string);
$arr = array(); // some big array with lots of dimensions
$ref = &$arr;

while ($key = array_shift($keys)) {
    $ref = &$ref[$key];
}

$ref = 'value';

这是做什么的:

  • 使用变量 $ref 来跟踪对 $arr 当前维度的引用。
  • 一次循环一个 $keys,引用当前引用的 $key 元素。
  • 将值设置为最终参考值。
$string = 'one/two/three/four';
$keys = explode('/', $string);
$arr = array(); // some big array with lots of dimensions
$ref = &$arr;

while ($key = array_shift($keys)) {
    $ref = &$ref[$key];
}

$ref = 'value';

What this is doing:

  • Using a variable, $ref, to keep track of a reference to the current dimension of $arr.
  • Looping through $keys one at a time, referencing the $key element of the current reference.
  • Setting the value to the final reference.
2024-12-13 01:10:12

您需要首先确保键存在,然后分配值。像这样的东西应该有效(未经测试):

function addValueByNestedKey(&$array, $keys, $value) {
    $branch = &$array;
    $key = array_shift($keys);
    // add keys, maintaining reference to latest branch:
    while(count($keys)) {
        $key = array_pop($keys);
        if(!array_key_exists($key, $branch) {
            $branch[$key] = array();
        }
        $branch = &$branch[$key];
    }
    $branch[$key] = $value;
}

// usage:
$arr = array();
$keys = explode('/', 'one/two/three/four');

addValueByNestedKey($arr, $keys, 'value');

You'll need to first make sure the key's exist, then assign the value. Something like this should work (untested):

function addValueByNestedKey(&$array, $keys, $value) {
    $branch = &$array;
    $key = array_shift($keys);
    // add keys, maintaining reference to latest branch:
    while(count($keys)) {
        $key = array_pop($keys);
        if(!array_key_exists($key, $branch) {
            $branch[$key] = array();
        }
        $branch = &$branch[$key];
    }
    $branch[$key] = $value;
}

// usage:
$arr = array();
$keys = explode('/', 'one/two/three/four');

addValueByNestedKey($arr, $keys, 'value');
平安喜乐 2024-12-13 01:10:12

这很老套,但是:

function setValueByArrayKeys($array_keys, &$multi, $value) {
     $m = &$multi
     foreach ($array_keys as $k){
         $m = &$m[$k];
     }
     $m = $value;
}

it's corny but:

function setValueByArrayKeys($array_keys, &$multi, $value) {
     $m = &$multi
     foreach ($array_keys as $k){
         $m = &$m[$k];
     }
     $m = $value;
}
两相知 2024-12-13 01:10:12
    $arr['one']['two']['three']['four'] = 'value';

    $string = 'one/two/three/four';
    $ExpCheck = explode("/", $string);
    $CheckVal = $arr;
    foreach($ExpCheck AS $eVal){
        $CheckVal = $CheckVal[$eVal]??false;
        if (!$CheckVal)
            break;
    }
    if ($CheckVal) {
        $val =$CheckVal;
    }

这将为您提供数组中的值。

    $arr['one']['two']['three']['four'] = 'value';

    $string = 'one/two/three/four';
    $ExpCheck = explode("/", $string);
    $CheckVal = $arr;
    foreach($ExpCheck AS $eVal){
        $CheckVal = $CheckVal[$eVal]??false;
        if (!$CheckVal)
            break;
    }
    if ($CheckVal) {
        $val =$CheckVal;
    }

this will give u your value in array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文