0

I'm trying to parse a string from a URL and convert it into a PHP array, but so far I haven't been able to get it to work correctly. The string contains comma separated values which are then grouped by periods, for example:

course.id,course.title,course.author,course.duration,course.trailer.video_key,course.trailer.duration,course.lessons.id,course.lessons.title,track.id,track.title,track.caption.srt,track.caption.vtt,track.caption.text

The Array equivalent to this string would be:

PHP:

$array = [
    'course' => [
        'id',
        'title',
        'author',
        'duration',
        'trailer' => [
            'video_key',
            'duration'
        ],
        'lessons' => [
            'id',
            'title'
        ]
    ],
    'track' => [
        'id',
        'title',
        'caption' => [
            'srt',
            'vtt',
            'text'
        ]
    ]
];

My current solution is this:

PHP:

$string = 'course.id,course.title,course.author,course.duration,course.trailer.video_key,course.trailer.duration,course.lessons.id,course.lessons.title,track.id,track.title,track.caption.srt,track.caption.vtt,track.caption.text';
$parameters = explode( ',', $string );

$array = array();
$inset = array();

foreach ( $parameters as $parameter ) {

    $segments = explode( '.', $parameter );

    if ( ! array_key_exists( $segments[0], $inset ) ) {
        $array[ $segments[0] ] = array();
        $inset[ $segments[0] ] = true;
    }

    $array[ $segments[0] ] = array_merge( $array[ $segments[0] ], addSegment( $segments, 1 ) );

}

function addSegment( $segments, $counter ) {

    $results = array();

    if ( end( $segments ) == $segments[ $counter ] ) {
        return array( $segments[ $counter ] => null );
    }

    $results[ $segments[ $counter ] ] = addSegment( $segments, $counter + 1 );

    return $results;

}

This somewhat works, however; it fails with simpler string like this one course,track,lessons

I think this calls for recursion but I'm not good at writing this so I'm hoping someone has already done this and can help me.

Thank you.

0

1 Answer 1

1

You can achieve this without recursion - just double loop and use of &:

$arr = explode(",", "course.id,course.title,course.author,course.duration,course.trailer.video_key,course.trailer.duration,course.lessons.id,course.lessons.title,track.id,track.title,track.caption.srt,track.caption.vtt,track.caption.text");
foreach($arr as $e) {
    $e = explode(".", $e);
    $obj = &$res; //set the current place root
    while(count($e) > 1) { // last element will be add as value
        $key = array_shift($e);
        if (!isset($obj[$key]))
            $obj[$key] = [];
        $obj = &$obj[$key]; //reset the current place
    }
    $obj[] = array_shift($e); // add the last as value to current place
}

$res will contain your desire output

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

5 Comments

Hello @dWinder, thank you for solution, it's close but the array is not in the format I need it to be. It's returning this gist.github.com/yojance/ad2ba51b1714bbb93eda4ce31e2ffbf5
@Yojance And what is the different? the only different I see is integer keys to all the elements you had in the array without keys so I assign integer as default keys - what is the expected behavior?
I also copied your $array and checked echo ($res === $array); which output TRUE
I guess I could do a combination of array_key_exists() or in_array to check for values with your current solution.
Of course you can. Moreover, my claim is you needed to use it for any solution as you expected output amd mine are the same

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.