"convert an array of strings, each string has dot separated values, to a multidimensional array" Answer’s

0

Try this.

<?php
$test = Array
(
    0 => 'INBOX.Trash',
    1 => 'INBOX.Sent',
    2 => 'INBOX.Drafts',
    3 => 'INBOX.Test.sub folder',
    4 => 'INBOX.Test.sub folder.test 2',
);

$output = array();
foreach($test as $element){
    assignArrayByPath($output, $element);   
}
//print_r($output);
debug($output);
function assignArrayByPath(&$arr, $path) {
    $keys = explode('.', $path);

    while ($key = array_shift($keys)) {
        $arr = &$arr[$key];
    }
}

function debug($arr){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}
Wednesday, March 31, 2021
 
Andres
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share