0

Using PHP, what would be the best way to convert a Plain Text List to an Associative Array.

The Plain Text List stucture looks like this:

Item 1
Item 1 > Sub Item 1.1
Item 1 > Sub Item 1.2
Item 2 
Item 2 > Sub Item 2.1 > Sub Sub Item 2.1.1
Item 2 > Sub Item 2.1 > Sub Sub Item 2.1.2
Item 2 > Sub Item 2.1 > Sub Sub Item 2.1.2 > Sub Sub Sub Item 2.1.2.1
Item 2 > Sub Item 2.2 > Sub Sub Item 2.2.1
Item 2 > Sub Item 2.2 > Sub Sub Item 2.2.2
Item 3
Item 4
Item 4 > Sub Item 4.1

The function / method would not be limited to a specific depth.

The desired array would look something like this:

$array['Item 1']['Sub Item 1.1'];
$array['Item 1']['Sub Item 1.2'];
$array['Item 2']['Sub Item 2.1'];
$array['Item 2']['Sub Item 2.1']['Sub Sub Item 2.1.1'];
$array['Item 2']['Sub Item 2.1']['Sub Sub Item 2.1.2'];
$array['Item 2']['Sub Item 2.1']['Sub Sub Item 2.1.2']['Sub Sub Sub Item 2.1.2.1'];
$array['Item 2']['Sub Item 2.2']['Sub Sub Item 2.2.1'];
$array['Item 2']['Sub Item 2.2']['Sub Sub Item 2.2.2'];
$array['Item 3'];
$array['Item 4'];
$array['Item 4']['Sub Item 4.1'];

Im not sure if this is the best array stucture to adopt.

On the UI Side...

The array will be used to populate a series of heirachal drop down lists through jQuery.ajax, where the contents of the child list is dependant on the parent selection.

User selects Item 2, a new list appears with the Sub Items, if they select Sub Sub Item 2.1.2 for instance, then a third list appears with Sub Sub Sub Item 2.1.2.1 as it's only option.

I can do all the jQuery stuff, its just the PHP function that is causing me a headache !

I included an explanation of what I wated to do with the array incase it influences the way the function(s) is(are) written.

1
  • I think i need to rework this idea. The array output isnt going lend itself to what i need to do with it. I appreciate your time and brain power... :D Commented Oct 17, 2013 at 11:24

1 Answer 1

2

Using explode, reference assigning and array_shift:

$input = "Item 1
Item 1 > Sub Item 1.1
...
Item 4 > Sub Item 4.1";

$lines = explode("\n", $input);
$out = array();
foreach ($lines as $line) {
    $parts = explode(" > ", $line);
    $ref = &$out;
    while (count($parts) > 0) {
        if (isset($ref[$parts[0]]) === false) {
            $ref[$parts[0]] = array();
        }
        $ref = &$ref[$parts[0]];
        array_shift($parts);
    }
}
echo "<pre>";
print_r($out);

Seems to give the correct output.

Sign up to request clarification or add additional context in comments.

2 Comments

What is complex about this? $ref walks the path you get from explode($line). I suppose you could write a recursive function instead but as you can see this works just as well.
this works fine, however i think i need to re-think my original idea, i think an array of the structure i set out will not work for what i want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.