array levels

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have to insert a string in an array at a particular level, creating
it if it doesn't exists.


$levels=array(a,b,c)   ***number of variables does vary

what I'd like to get


The final array is being filled in a foreach loop therefore $levels
and $name change at every loop.

How can I do this?

Re: array levels

Quoted text here. Click to load it want your $levels array to be a "map" of sorts, providing the
dimensions of the array?
and you want to push a $name value at the end of this, so you have n-
dimension array, every dimension only one value deep containing either
another array or the $name value?

$final_array = array();
foreach ($levels as $key=>$value) {
  $array[$value] = array();
  $final_array[] = $array(); // or array_push()
$final_array[] $name;


Re: array levels

On 14.08.2007 15:45 Luca Cioria wrote:
Quoted text here. Click to load it

For example, with a simple recursive function

function hierarchize($ary, $value) {
    return empty($ary) ? $value :
        array(array_shift($ary) =>
            hierarchize($ary, $value));

# example

        array('a', 'b', 'c'), 'myName'));

Hope you like the name. ;)

gosha bine

makrell ~
php done right ;)

Site Timeline