Bluehost.com Web Hosting $6.95

adding keys to an array

This is a discussion on adding keys to an array within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi all I am in problem with this script i have a key list and a value $key_list = array('key0','...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-24-2003
Le-Goret
 
Posts: n/a
Default adding keys to an array

Hi all
I am in problem with this script
i have a key list and a value
$key_list = array('key0','key1','key2','key3');
$val = 'value';

and I want this result
$foo=array(
'key3' => array(
'key2' => array(
'key1' => array(
'key0' => 'val'
)
)
)
);

Is there anyone who have find a way to do so ?


Reply With Quote
  #2 (permalink)  
Old 09-25-2003
Dmitry Ruban
 
Posts: n/a
Default Re: adding keys to an array

Hey Le-Goret,

This could help:

$key_list = array('key0','key1','key2','key3');
$val = 'value';

$res = _func ( $key_list, $val );

function _func ( $arr, $val )
{
$key = array_pop( $arr );
if( sizeof($arr) )
return array($key => _func($arr,$val) );
else
return array($key => $val );
}

Dima

"Le-Goret" <le-goret_no_@_spam_tyteca.net> wrote in message
news:3f7171e7$0$2806$626a54ce@news.free.fr...
> Hi all
> I am in problem with this script
> i have a key list and a value
> $key_list = array('key0','key1','key2','key3');
> $val = 'value';
>
> and I want this result
> $foo=array(
> 'key3' => array(
> 'key2' => array(
> 'key1' => array(
> 'key0' => 'val'
> )
> )
> )
> );
>
> Is there anyone who have find a way to do so ?
>
>



Reply With Quote
  #3 (permalink)  
Old 09-25-2003
Le-Goret
 
Posts: n/a
Default Re: adding keys to an array

Thank you very much :)
you have the firstname of my brother I see it like a sign :)

"Dmitry Ruban" <dima@region35.ru> a écrit dans le message de
news:bku19i$1geg$1@gavrilo.mtu.ru...
> Hey Le-Goret,
>
> This could help:
>
> $key_list = array('key0','key1','key2','key3');
> $val = 'value';
>
> $res = _func ( $key_list, $val );
>
> function _func ( $arr, $val )
> {
> $key = array_pop( $arr );
> if( sizeof($arr) )
> return array($key => _func($arr,$val) );
> else
> return array($key => $val );
> }
>
> Dima
>
> "Le-Goret" <le-goret_no_@_spam_tyteca.net> wrote in message
> news:3f7171e7$0$2806$626a54ce@news.free.fr...
> > Hi all
> > I am in problem with this script
> > i have a key list and a value
> > $key_list = array('key0','key1','key2','key3');
> > $val = 'value';
> >
> > and I want this result
> > $foo=array(
> > 'key3' => array(
> > 'key2' => array(
> > 'key1' => array(
> > 'key0' => 'val'
> > )
> > )
> > )
> > );
> >
> > Is there anyone who have find a way to do so ?
> >
> >

>
>



Reply With Quote
Reply
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

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




All times are GMT +1. The time now is 12:21 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0