Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Collection, how to obtain array of values of a particular field

Tags:

magento

Starting from a Collection, I need to obtain an array of values of a particular field..let me call this field my_id.

To do this, I use the Varien_Data_Collection::setDataToAll() on my Collection..in this way:

$collection_fields_array = $collection->setDataToAll(array('my_id'))->toArray(array('my_id'));

..what I obtain is something like this:

Array
(
    [0] => Array
        (
            [my_id] => 71e1bd18
        )

    [1] => Array
        (
            [my_id] => 70d47a69
        )

    [2] => Array
        (
            [my_id] => 687bed84
        )

    [3] => Array
        (
            [my_id] => 673df159
        )

    [4] => Array
        (
            [my_id] => 66690a4c
        )

    [5] => Array
        (
            [my_id] => 65994440
        )
)

But..if my Collection contains a large number of items..setDataToAll() becomes too time consuming and finally crashes all 2Gb(!!!) of memory...this is because its iteration between all items.

Is another way to obtain an array of field values without this workaround?

like image 682
Zoba82 Avatar asked Jun 06 '13 14:06

Zoba82


1 Answers

In this case,you can use getColumnValues('fieldName').

So,try this

$collection->getColumnValues('my_id')

like image 93
Igor Sydorenko Avatar answered Nov 09 '22 05:11

Igor Sydorenko