Skip to content
Advertisement

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

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;

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement