Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two questions about a function 3

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys :)

In the following function ...

1. how do I prevent $array_flat from being reset during the recursion without using $GLOBALS/global or sessions ?

2. how could I grab the name of the function currently running so that I don't have to state "hd_array_multi_to_flat" a second time?

Thanks a lot!

Code:
function hd_array_multi_to_flat($array_multi, $where = "array") {

    while (list($key,$value) = each($array_multi)) {
    
        if (is_array($value)) {
        
        hd_array_multi_to_flat($value, $where . "[$key]");
        
        } else {
        
            for ($i=0; $i<count($value);$i++) {
            
            $array_flat[] = $key;
            $array_flat[] = $value;

            }
        
        }
    
    }
    
return $array_flat;
  
}
 
Nice!
How about the first question? ;)
 
Hi

Well, with that you have to wait for a real MVP, is not enough an opportunist...

Sorry, I do almost no PHP since a long time, some of the solutions are not flashing in as before...

Feherke.
 
No worries, Feherke :)

To anyone else ... I've managed to do what I want but I get this error message : Warning: Missing argument 3 for hd_array_multi_to_flat()

And &$array_flat = NULL doesn't work either :(

Code:
function hd_array_multi_to_flat($array_multi, $where = "array", &$array_flat) {

    while (list($key,$value) = each($array_multi)) {
    
        if (is_array($value)) {
        
        $array_flat[] = $key;
        
        $function = __FUNCTION__;
        
        $function ($value, $where . "[$key]", &$array_flat);
        
        } else {
        
            for ($i=0; $i<count($value);$i++) {
            
            $array_flat[] = $value;

            }
        
        }
    
    }
    
return $array_flat;
  
}
 
BTW, it's important to add that I want to be able to call the function like this for the very first call :

hd_array_multi_to_flat($some_array);

I'd like to avoid any extra argument.

Thanks again :)
 
have a look at static variables and, perhaps, consider also creating a static var inside the function for the current array index.

am on holiday at the mo otherwise i'd mock up a bit of code for you!
 
Thanks for reminding me of static! ;)

It works now !

Code:
function hd_array_multi_to_flat($array_multi, $where = "array") {

    if (!$array_flat) static $array_flat;

    while (list($key,$value) = each($array_multi)) {
    
        if (is_array($value)) {
        
        $array_flat[] = $key;
        
        $function = __FUNCTION__;
        
        $function ($value, $where . "[$key]");
        
        } else {
        
            for ($i=0; $i<count($value);$i++) {
            
            $array_flat[] = $value;

            }
        
        }
    
    }
    
return array_unique($array_flat);
  
}

Et bonnes vacances! ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top