NYCPHP Meetup

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

csnyder chsnyder at gmail.com
Thu Jan 4 11:39:08 EST 2007


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