PHP: Set value of nested array using variable as key

后端 未结 3 1485
情歌与酒
情歌与酒 2020-12-11 07:28

Lets say i have this kind of code:

    $array = [
        \'a\'=> [
            \'b\' => [
                \'c\'=>\'some value\',
            ],
            


        
3条回答
  •  忘掉有多难
    2020-12-11 07:33

    Easiest way to do this would be using set method from this library:

    Arr::set($array, 'a.b.c', 'new_value');
    

    alternatively if you have keys as array you can use this form:

    Arr::set($array, ['a', 'b', 'c'], 'new_value');
    

提交回复
热议问题