#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Question How to convert from a group of strings to a multi-dim array?


    I would like to convert an array of strings ('AAAB', 'AAAC'..etc) into a multi-dimensional array.

    Here is some sample input:
    AAAB
    AAAC
    AAAD
    ABAA

    Sample output:
    PHP Code:
    Array
    (
        [
    A] => Array
            (
                [
    A] => Array
                    (
                        [
    A] => Array
                             (
                              [
    B] => 0
                              
    [C] => 0
                              
    [D] => 0
                             
    )
                    )

                [
    B] => Array
                    (
                        [
    A] => Array
                             (
                              [
    A] => 0
                             
    )
                    )

            )


    I'm sure a recursive function would work, but I'm having trouble wrapping my mind around how to do it. Could someone help me out? Thanks!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Feel like delving into strange code?
    PHP Code:
    $root = array();
    $strings = array("AAAB""AAAC""AAAD""ABAA");

    // translate into a stack
    $stack = array();
    foreach (
    $strings as $string) {
        
    $stack[] = array(&$root$string);
    }

    // process the stack
    // each item is an array that looks like
    //    array(&array to add into, string to add)
    while ($stack) {
        
    $top array_shift($stack);
        
    $letter substr($top[1], 01);
        
    $rest substr($top[1], 1);

        
    // if there is only one letter left, add it
        
    if ($rest == "") {
            
    $top[0][$letter] = 0;
        }
        
    // otherwise make sure there's an array for the first letter
        
    else {
            if (!isset(
    $top[0][$letter])) {
                
    $top[0][$letter] = array();
            }
            
    // and add something to the stack to process the rest of the string
            
    $stack[] = array(&$top[0][$letter], $rest);
        }
    }

    print_r($root); 
    It works because of references, allowing you to modify something in the original $root array even though you're going through different variables.

    As for the recursive solution, you take basically the same idea but instead of a stack you have function calls. It returns an array for a string: if the string is empty then return a 0, otherwise return an array with first letter => recursive call using the remainder of the string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0
    Thanks! I think I see what you are doing, and it will take a little while to digest. I tried your code and got "ErrorException [ Warning ]: Cannot use a scalar value as an array", on the $top[0] line quoted below.

    Originally Posted by requinix
    // otherwise make sure there's an array for the first letter
    else {
    if (!isset($top[0][$letter])) {
    $top[0][$letter] = array();
    }
    [/php]
    But the error only occurs when a I pass it various length strings. When they are all the same, it works fine.

    Thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo