NYCPHP Meetup

NYPHP.org

[nycphp-talk] autovivification/style question

Chris Snyder chsnyder at gmail.com
Thu Mar 17 12:43:09 EDT 2011


On Thu, Mar 17, 2011 at 12:35 PM, David Mintz <david at davidmintz.org> wrote:
> Is there anything wrong with something like...
>
>   $data = array();
>  $results = $db->query($sql);
>
>   while ($row = $results->fetch()) {
>        // ... this right here:
>        $data[$row->id][] = $row->someStringValue
>  }
>
> You can see I am trying to build up an array of arrays whose the keys are id
> numbers and whose values are arrays of strings. I have tried it and it works
> fine. I just wonder if it's kosher as a matter of cleanliness and style.

To avoid future warnings, I'd probably throw an if ( !isset($data[
$row->id ]) ) { $data[ $row->id ] = array(); } in there.



More information about the talk mailing list