How to recursively create a multidimensional array?

前端 未结 5 1757
无人及你
无人及你 2020-12-17 06:47

I am trying to create a multi-dimensional array whose parts are determined by a string. I\'m using . as the delimiter, and each part (except for the last) shoul

5条回答
  •  一个人的身影
    2020-12-17 07:40

    I say split everything up, start with the value, and work backwards from there, each time through, wrapping what you have inside another array. Like so:

    $s = 'config.debug.router.strictMode = true';
    list($parts, $value) = explode(' = ', $s);
    
    $parts = explode('.', $parts);
    while($parts) {
       $value = array(array_pop($parts) => $value);
    }
    
    print_r($parts);
    

    Definitely rewrite it so it has error checking.

提交回复
热议问题