PHP: Set value of nested array using variable as key

后端 未结 3 1479
情歌与酒
情歌与酒 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

    Hi bro you can do it like this throught an array of keys :

    This is your array structured :

    $array = array(
        'a'=> array(
            'b' => array(
                'c'=>'some value',
            ),
        ),
    );
    

    This is the PHP code to get value from your array with dynamic keys :

    $result = $array; //Init an result array by the content of $array
    
    $keys = array('a','b','c'); //Make an array of keys
    
    //For loop to get result by keys
    for($i=0;$i

    I hope that the answer help you, Find here the PHPFiddle of your working code.

提交回复
热议问题