NYCPHP Meetup

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

Mark Armendariz enolists at gmail.com
Thu Jan 4 15:11:24 EST 2007


decided to play with this a bit.  first time i've ever found myself using
variable variables.  dunno if it helps at all, but here's what i came up
with (4.4.1, but should apply anywhere).  it seems you can't access the
array values directly, but copying it works.  You could also use var vars
for object properties instead if you wanted to go that route.  It seems to
me this could be nested as deeply as necessary as well.

<?php
    include_once 'base/function.debug.php';
    dbg($_POST, 'Post Array');

    class foo {
        function foo() {}
    }

    $foo = new foo;
    $foo->bar = array( 0=>'quiz', 1=>'quuz', 2=>'quux' );

    // loop through our posted vars
    foreach(array_keys($_POST) as $sObject) {
        // if we've posted an object, open'r up
        if (is_object($$sObject)) {
            dbg($$sObject, $sObject);

            foreach($_POST[$sObject] as $sProperty => $mValue) {
                // print out the property - boo, which is an array
                dbg($$sObject->$sProperty, $sObject . '->' . $sProperty);

                // try showing the array's value
                dbg($$sObject->$sProperty[$mValue], 'Direct Access - throws
error');

                // now copy the array and then show it
                $theArray = $$sObject->$sProperty;
                dbg($theArray[$mValue], 'Copied Array - should work');
            }
        }
    }
?>

<form method="post">
    <?php foreach($foo->bar as $mValue => $sDisplay) { ?>
        <label for="foo_bar_<?= $mValue ?>">
            <input type="radio"
                   name="foo[bar]"
                   value="<?= $mValue ?>"
                   id="foo_bar_<?= $mValue ?>"
                   <?= ($mValue == $_POST['foo']['bar']) ?
'checked="checked"' : '' ?> /> <?= $sDisplay ?>
        </label><br />
    <?php } ?>
    <input type="submit" />
</form>



and my debug function:

<?php
function dbg($mVar, $sTitle = '') {
        $sReturn = '';

        if (strlen($sTitle)) {
            $sReturn .= $sTitle .= ': ';
        }

        $sReturn .= '<pre>' . print_r($mVar, 1) . '</pre>';
}
?>


On 1/4/07, Tom Melendez <tom at supertom.com> wrote:
>
> On 1/4/07, Nate Abele <nate at cakephp.org> wrote:
> > 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?
>
> That's what I came up with too.
> $key = "bar[1]";
> preg_match("/(.*)\[(\d)\]/",$key,$m);
> $name=$m[1];
> $index=$m[2];
> $myarray=$foo->$name;
> print $myarray[$index];
>
> Very interested in other solutions.  If bar comes in as an array (like
> $_POST[bar][1]) you could probably use array_keys to suck out keys,
> although I haven't tried it.
>
> Tom
> http://www.liphp.org
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070104/c8b9a626/attachment.html>


More information about the talk mailing list