0

I know there are solutions for getting one column as an array.

$collection->getColumnValues('column_name');

And adding limit without setPageSize() and setCurPage()

$collection->getSelect()->limit(3)

But I want both.

My temp solution is I'll run foreach loop over the collection from limit statement and just fetch the column in the array as per the requirement.

1 Answer 1

1

Find the below code which works fine for me.

$collection = $customObj->getCollection()->addFieldToSelect('column_name');
$collection->getColumnValues('column_name');
$collection->getSelect()->limit(3);
echo '<pre>', print_r($collection->getData()); exit;

//Output : 

Array
(
    [0] => Array
        (
            [custom_field_1] => Sample Text 1 for Data 1
        )

)

As per your comment, I have updated my code which also working fine.

$customData = [];
$collection = $customObj->getCollection()->addFieldToSelect('column_name');
$collection->getSelect()->limit(3);
$customData = $collection->getColumnValues('column_name');
echo '<pre>', print_r($customData); exit;

//Output :

Array
(
    [0] => Sample Text 1 for Data 1
)
2
  • Yes, you are almost there, just need to interchange the sequence of the 2nd and 3rd statements and then assign the variable for the array for getColumnValues that will work. I've got a reference from this ans - magento.stackexchange.com/a/145655/45519
    – Knight017
    Commented Feb 17, 2023 at 18:21
  • Hi, As per your comment, I have incorporated your changes and updated my answer. If you find my answer useful then accept it, So it will help other community users. Commented Feb 18, 2023 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.