TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-28-2008, 01:01 AM   #1 (permalink)
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
Old 05-28-2008, 12:00 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

PHP Code:
function make_multi($flat)
{
    
$multi = array();
    
$temp  =& $multi;
    foreach (
$flat as $item)
    {
        
$temp[$item] = array();
        
$temp =& $temp[$item];
    }
    return 
$multi;
}

// Quicky test
$test  = array('0''15''32''18');
$multi make_multi($test);
var_dump($multi); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
drewbee (05-28-2008)
Old 05-28-2008, 11:38 PM   #3 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

Awesome, thanks mate!
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design