|
Posted by gosha bine on 08/14/07 15:11
On 14.08.2007 15:45 Luca Cioria wrote:
> I have to insert a string in an array at a particular level, creating
> it if it doesn't exists.
>
> example:
>
> $levels=array(a,b,c) ***number of variables does vary
> $name="my_name"
>
> what I'd like to get
>
> $final_array=(a=>array(b=>array(c=>"my_name")))
>
>
> The final array is being filled in a foreach loop therefore $levels
> and $name change at every loop.
>
> How can I do this?
>
For example, with a simple recursive function
function hierarchize($ary, $value) {
return empty($ary) ? $value :
array(array_shift($ary) =>
hierarchize($ary, $value));
}
# example
print_r(
hierarchize(
array('a', 'b', 'c'), 'myName'));
Hope you like the name. ;)
--
gosha bine
makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
[Back to original message]
|