php – One-liner to fetch data in a loop / iterator and store into a variable-ThrowExceptions

Exception or error:

Is there some magic one-liner to get this data ?

while ($res->next())
{
    $this->data['DwellingUnit'][] = $res->row;
}

Not while ($res->next()) $this->data['DwellingUnit'][] = $res->row;

How to solve:

I presume iterator_to_array can be used with array_column:

$this->data['DwellingUnit'] = array_column(
    iterator_to_array($res),
    'row'
);

As array_column can extract values from array of objects too, but since php7.

As a con: here’re two loops – one over iterator to get an array, second is over array to extract row property.

Leave a Reply

Your email address will not be published. Required fields are marked *