[UPHPU] pluck out of array with index without rebuilding

Sean Thayne sean at exit12.org
Thu Jan 22 11:31:48 MST 2009


Nice function, thanks ;)

On Thu, Jan 22, 2009 at 11:29 AM, Matthew Frederico <mfrederico at gmail.com>wrote:

> On Fri, Jan 16, 2009 at 11:06 AM, Joseph Scott
> <joseph at randomnetworks.com> wrote:
> >
> > On Jan 15, 2009, at 9:29 PM, Wade Preston Shearer wrote:
> >
> >> But if my food array looks like this…
> >>
> >> $food[] = array('id' => 1, 'name' => 'banana');
> >> $food[] = array('id' => 2, 'name' => 'taco');
> >> $food[] = array('id' => 3, 'name' => 'toast');
>
>
> Had that same issue .. I wrote a function and go about it like this:
>
> <?php
> $food[] = array('id' => 1, 'name' => 'banana');
> $food[] = array('id' => 2, 'name' => 'taco');
> $food[] = array('id' => 3, 'name' => 'toast');
>
> print_r($food);
> // Using 'name' key
> print_r(setIndex($food,'name'));
>
> // Using 'id' key
> print_r(setIndex($food,'id'));
>
>    /**
>    * setIndex
>    *
>    * @description reindex an array to one of the key values
>    * @returns array
>    **/
>    function setIndex($array,$newIdx)
>    {
>        $newArray = array();
>        foreach($array as $value)
>        {
>            $newArray[$value[$newIdx]] = $value;
>        }
>        return($newArray);
>    }
>
>
> ?>
>
>
> --
> --
> -- Matthew Frederico
>
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>


More information about the UPHPU mailing list