Re: how do i get assosciative name in foreach

From: Date: Fri, 08 Aug 2003 18:03:34 +0000
Subject: Re: how do i get assosciative name in foreach
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-158905@lists.php.net to get a copy of this message
$arr = array('key' => 'value'); foreach ($arr as $k => $v) { echo "$k is the name/key, $v is the value"; } Regards, Philip On 8 Aug 2003, Dan Anderson wrote: > I want to send an assosciative array to a foreach loop. Is there any > way to get the name? > > For instance, now I have: > > <?php > > $array['element1']['name'] = 'element1'; > $array['element1']['value'] = 'value1'; > $array['element2']['name'] = 'element2'; > $array['element2']['value'] = 'value1'; > > foreach ($array as $element) > { > if ($element['name'] == 'element1') > { do_something(); } > elseif ($element['name'] == 'element1') > { do_somethingelse(); } > } > > ?> > > I want to do: > > <?php > > $array['element1'] = 'element1'; > $array['element2'] = 'element2'; > > foreach ($array as $element) > { > // assuming get_assoc gets the assosciative name > if (get_assoc($element) == 'element1') > {get_assoc($element) do_something(); } > elseif ( == 'element1') > { do_somethingelse(); } > } > > ?> > > Is what I want possible? > > Thanks in advance, > > Dan > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#158905) next »