View Single Post
Old 05-28-2008, 01:01 AM   #1 (permalink)
drewbee
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Calculator Build multi-dimensional array out of a flat array

Hi All,

I am having some issues doing this. I think the only way to accomplish this is to use a recursive function, however I am failing miserably.

Basically, I need to turn a flat array into a multi-dimensional array.

The flat arrays *values* need to become the new *keys* to the multi-dimensional array.

The array I feed to it can have 1 value in it, up to nth high.
Example:
PHP Code:
// Starts out as
$array = array(=> '0',
                       
=> '15',
                       
=> '32',
                       
=> '18');

// Is turned into
$array = array('0' => 
                                array(
'15' =>
                                                    array(
'32' =>
                                                                         array(
'18' => array()
                                                                                 )
                                                            )
                                        )
                      ); 
As I said though, my attempts have failed with all kinds of unknown indexes and the like.

PHP Code:
    function recursiveBuildArray($array, &$temp)
    {
        if (isset(
$array['0']))
        {
            
$temp[$array['0']] &= array();
            
$id $array['0'];
            unset(
$array['0']);
            
$this->recursiveBuildArray($array[$id], &$temp);
        }
        else
        {
            return 
false;
        }
    } 
Any help on this one?
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote