Profile
Profile

Reputation: 45

Convert string containing delimited keypaths to a hierarchical associative array

Say I have a string as such; a.b.c|a.b.d|a.b.e|f|f.g|f.h|i, I want to create a new array (or object) as the following

A > B > C, D, E    
F > G, H    
I

With > being a nested array, , being an element in the parent array.

These should be able to continue nesting a multitude of times, e.g. A > B > C > D > E > F, D

I've tried exploding the string and then those strings to an array - this array holds A > B > C, A > B > D, A > B > E etc., I just can't get my head around to how to combine them efficiently.

I started with just looping over each element in the array and checking if the parents' key exists, but that was failing.

Upvotes: 1

Views: 70

Answers (2)

Profile
Profile

Reputation: 45

Want to thank Richard for his solution, which allowed me to achieve my aim.

See the code below:

public static function permissions($permList) {
    $segments = explode('|', $permList);
    $md_array = array();
    foreach($segments as $segment) {
        $current = &$md_array;
        $tokens = explode('.', $segment);
        $x = 0;
        foreach($tokens as $token) {                
            if(!array_key_exists($token, $current) ) {
                if(count($tokens) > $x+1)
                    $current[$token] = array();
                else
                    $current[] = $token;
            }
            if(count($tokens) > $x+1)
                $current = &$current[$token];
            $x++;
        }
    }
    return $md_array;
}   

If I input a.b.c|a.b.d|a.b.e|f|f.g|f.h|i, I am returned with the expected output above.

Many thanks.

Upvotes: 0

Richard
Richard

Reputation: 1055

<?php

// read line from standard input
$line = trim(fgets(STDIN));

echo "Line: $line\n"; // debug

// split lines by |
$segments = explode('|', $line);

print_r($segments); // debug

// prepare output array
$md_array = array();

// walk through the segments
foreach($segments as $segment) {
    // set pointer to output array
    $current = &$md_array;
    // split segment by .
    $tokens = explode('.', $segment);

    print_r($tokens); // debug

    foreach($tokens as $token) {
        echo "working on $token\n";

        // if key is not included in the array, create empty array
        if( ! array_key_exists($token, $current) ) {
            $current[$token] = array();
        }
        // pass the pointer to following sub-section
        $current = &$current[$token];
    }
}

// print out the output
print_r($md_array);

?>

Testing the script

echo "a.b.c|a.b.d|a.b.e|f|f.g|f.h|i" | php test.php

Output

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                        )

                    [d] => Array
                        (
                        )

                    [e] => Array
                        (
                        )

                )

        )

    [f] => Array
        (
            [g] => Array
                (
                )

            [h] => Array
                (
                )

        )

    [i] => Array
        (
        )

)

Upvotes: 1

Related Questions