0

Currently I'm converting my collection data to json_encode like this:

$collections = $this->modelFactory->create()->getCollection()
                   ->addFieldToFilter('status','1');
$result = array();
foreach($collections as $collection){
  $result[] = $collection->getData();
}
return json_encode($result);

The problem is I need to loop for each collection to get the array data so that I can put it inside json_encode function, is there a way I can convert the collection result to json_encode without looping?

1 Answer 1

4

Try this:

$result = $collections->toArray();

return json_encode($result);

or

$result = $collections->getData();
return json_encode($result);

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.