Re: Passing array names into a function
| From: | Brian Clark | Date: | Wed, 05 Sep 2001 20:00:24 +0000 |
| Subject: | Re: Passing array names into a function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-66097@lists.php.net to get a copy of this message | ||
Hi Geoff,
@ 3:30:24 PM on 9/5/01, Geoff Caplan wrote:
> I think I am being dumb but I just can't figure out a way to do this:
> I have a static array inside a function, and I want caller to be able to
> set/get values in the array.
> Something like this:
> <?php
> data_store( "set", "my_array['key1']['key2']", "my
> new value")
> function data_store( $action, $variable, $value )
> {
> static my_array = array( ) ;
> if( $action == "set" )
> {
> // set my new value
> }
> }
?>>
> I have been mucking about with eval( ) and with variable variables, but
> can't get it to work.
> Can some kind person point out what I am missing??
Is this what you're looking for? (sorry so messy; in a hurry)
<?php
$my_array = array('foo' => array('bar' => 'baz'));
$my_var = 'peanuts';
print "<br>Before: " . $my_array[foo][bar];
print "<br>Before: " . $my_var;
function data_store($action, &$variable, $value )
{
if($action == 'set')
return $variable = $value;
}
data_store('set',$my_array[foo][bar],'ack');
data_store('set',$my_var,'peanut butter');
print "<br>After: " . $my_array[foo][bar];
print "<br>After: " . $my_var;
?>
-Brian
--
PGP is spoken here: 0xE4D0C7C8
Please, DO NOT carbon copy me on list replies.