How to recursively create a multidimensional array?

前端 未结 5 1627
无人及你
无人及你 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:26

    Gumbo's answer looks good.

    However, it looks like you want to parse a typical .ini file.

    Consider using library code instead of rolling your own.

    For instance, Zend_Config handles this kind of thing nicely.

    0 讨论(0)
  • 2020-12-17 07:33
    // The attribute to the right of the equals sign
    $rightOfEquals = true; 
    
    $leftOfEquals = "config.debug.router.strictMode";
    
    // Array of identifiers
    $identifiers = explode(".", $leftOfEquals);
    
    // How many 'identifiers' we have
    $numIdentifiers = count($identifiers);
    
    
    // Iterate through each identifier backwards
    // We do this backwards because we want the "innermost" array element
    // to be defined first.
    for ($i = ($numIdentifiers - 1); $i  >=0; $i--)
    {
    
       // If we are looking at the "last" identifier, then we know what its
       // value is. It is the thing directly to the right of the equals sign.
       if ($i == ($numIdentifiers - 1)) 
       {   
          $a = array($identifiers[$i] => $rightOfEquals);
       }   
       // Otherwise, we recursively append our new attribute to the beginning of the array.
       else
       {   
          $a = array($identifiers[$i] => $a);
       }   
    
    }
    
    print_r($a);
    
    0 讨论(0)
  • 2020-12-17 07:35

    Let’s assume we already have the key and value in $key and $val, then you could do this:

    $key = 'config.debug.router.strictMode';
    $val = true;
    $path = explode('.', $key);
    

    Builing the array from left to right:

    $arr = array();
    $tmp = &$arr;
    foreach ($path as $segment) {
        $tmp[$segment] = array();
        $tmp = &$tmp[$segment];
    }
    $tmp = $val;
    

    And from right to left:

    $arr = array();
    $tmp = $val;
    while ($segment = array_pop($path)) {
        $tmp = array($segment => $tmp);
    }
    $arr = $tmp;
    
    0 讨论(0)
  • 2020-12-17 07:37

    I really like JasonWolf answer to this.

    As to the possible errors: yes, but he supplied a great idea, now it is up to the reader to make it bullet proof.

    My need was a bit more basic: from a delimited list, create a MD array. I slightly modified his code to give me just that. This version will give you an array with or without a define string or even a string without the delimiter.

    I hope someone can make this even better.

    $parts = "config.debug.router.strictMode";
    
    $parts = explode(".", $parts);
    
    $value = null;
    
    while($parts) {
      $value = array(array_pop($parts) => $value);
    }
    
    
    print_r($value);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题