SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question String to multi-dimensional Array

    Hello all,

    I know how to turn a string into an array where the parts of the string become array keys:

    Code PHP:
    $string = 'one_two_three';
    $array  = array_flip(explode('_', $string));
    print_r($array);
    Code:
    Array
    (
        [one] => 0
        [two] => 1
        [three] => 2
    )
    But how could I turn that string into a multi-dimensional array like this:
    Code:
    Array
    (
        [one] => Array
            (
                [two] => Array
                    (
                        [three] => 2
                    )
    
            )
    
    )
    ... btw. the values are not important for now.

    Thanks for any help.

    oerdec

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function make_deep($a) {
        return 
    count($a) ?
            array(
    array_shift($a) => make_deep($a)) :
            
    'some_val'
            
    ;
    }

    $a = array('aa''bb''cc');
    print_r(make_deep($a)); 
    ok?

  3. #3
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, thank you stereofrog. This is exactly what I needed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •