# How to go from results array of objects to array for csv?

**URL:** <https://discourse.cakephp.org/t/how-to-go-from-results-array-of-objects-to-array-for-csv/4619>\
**Category:** Need Help\
**Created:** [July 24, 2018, 5:27pm UTC](https://discourse.cakephp.org/t/how-to-go-from-results-array-of-objects-to-array-for-csv/4619 "2018-07-24T17:27:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakim](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hakim](https://discourse.cakephp.org/u/hakim)\
**Post date:** [July 24, 2018, 5:27pm UTC](https://discourse.cakephp.org/t/how-to-go-from-results-array-of-objects-to-array-for-csv/4619/1 "2018-07-24T17:27:29Z")

</div>

Is there an easier way to go from a results object to a flatter array for a csv?

```
$this->loadModel('AdverseEvents.Queries');
    $results= $this->Queries->find('all')
            ->select([
                'Queries.crf_adverse_event_id','CountryLink.id','CountryData.name','Sites.name', 
                'CrfAdverseEvents.participant_id', 'CrfAdverseEvents.medical_terms',
                'CrfAdverseEvents.date_onset', 'Queries.type', 'Queries.title', 
                'Queries.status','Queries.message', 'Queries.created'])
            ->contain([
                'CrfAdverseEvents' => [    
                    'Sites' => ['CountryLink' => ['CountryData'] ]
                ]
            ])
            ->order(['Queries.created' => 'DESC'])
            ->all()
            ->toArray();

    $table = [];
    foreach ($results as $query) {
        $table[] = [
            $query->crf_adverse_event_id,
            $query->crf_adverse_event->site->country_link->country_data->name,
            $query->crf_adverse_event->site->name,
            $query->crf_adverse_event->participant_id,
            $this->escapeForCsv($query->crf_adverse_event->medical_terms),
            $query->crf_adverse_event->date_onset,
            $query->type,
            $this->escapeForCsv($query->title),
            $query->status,
            $this->escapeForCsv($query->message),
            $query->created->format('Y-m-d H:s:i')
        ];
    }
```

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 25, 2018, 2:20pm UTC](https://discourse.cakephp.org/t/how-to-go-from-results-array-of-objects-to-array-for-csv/4619/2 "2018-07-25T14:20:45Z")

</div>

Use this package: [https://github.com/FriendsOfCake/cakephp-csvview](https://github.com/FriendsOfCake/cakephp-csvview)

---

<div class="post-metadata">

**Author:** ![hakim](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hakim](https://discourse.cakephp.org/u/hakim)\
**Post date:** [August 17, 2018, 9:24am UTC](https://discourse.cakephp.org/t/how-to-go-from-results-array-of-objects-to-array-for-csv/4619/3 "2018-08-17T09:24:22Z")

</div>

Is there a way using CakePHP’s hash functions?
