Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 12th, 2013, 08:25 PM
 Suthern101
Registered User

Join Date: Feb 2009
Posts: 4
Time spent in forums: 39 m 24 sec
Reputation Power: 0
PHP-General - 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
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
March 12th, 2013, 09:11 PM
 requinix
Forgetful

Join Date: Mar 2007
Location: Washington, USA
Posts: 13,512
Time spent in forums: 5 Months 2 Weeks 2 Days 9 h 16 m 24 sec
Reputation Power: 9259
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], 0, 1);     \$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.

#3
March 13th, 2013, 11:26 AM
 Suthern101
Registered User

Join Date: Feb 2009
Posts: 4
Time spent in forums: 39 m 24 sec
Reputation 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.

Quote:
 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!

 Viewing: Dev Shed Forums > Programming Languages > PHP Development > PHP-General - How to convert from a group of strings to a multi-dim array?