NYCPHP Meetup

NYPHP.org

[nycphp-talk] Re: Passing array elements as... strings?

Nate Abele nate at cakephp.org
Thu Jan 4 12:15:33 EST 2007


Two solutions that come to mind are to (a) parse out the var name and  
the index from the string, or (b) submit the var name and the index  
as two separate POST vars.  Any reason either of those wouldn't work?

- Nate

> Date: Thu, 4 Jan 2007 11:39:08 -0500
> From: csnyder <chsnyder at gmail.com>
> Subject: [nycphp-talk] Passing array elements as... strings?
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Message-ID:
> 	<b76252690701040839j18397c74v4695ae4892bbde77 at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> Let's say you've got an object, with a property that contains an array
> with several values:
>
> $foo->bar = array( 0=>'quiz', 1=>'quuz', 2=>'quux' );
>
> You want to get the value of $foo->bar[1], but all you have is the
> string "bar[1]" to work with (because it's a key in a submitted form),
> as $key = "bar[1]";
>
> The classic, and deeply abhorrent, way to do this is:
> eval( "\$value = \$foo->$key;" );
>
> I thought that $value = $foo->{$key} might work, but it doesn't.
>
> Does anyone have a pattern to solve this?
>
> -- 
> Chris Snyder
> http://chxo.com/




More information about the talk mailing list