# Entity object to array

**URL:** <https://discourse.cakephp.org/t/entity-object-to-array/7813>\
**Category:** Need Help\
**Created:** [May 12, 2020, 9:44pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813 "2020-05-12T21:44:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [May 12, 2020, 9:44pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/1 "2020-05-12T21:44:00Z")

</div>

I stored in session object entity, is it possible to convert this object back to an array?  
Because the patch entity does not merge the data.

```
$data = [
    'id' => 1,
    'users_documents_items' => $ContentArray['users_documents_items'], **<< this is object entity, if it array - patch entity works, if entity object, doestn patch entity...**
];
     
$ContentUserDocument = $this->UsersDocuments->newEmptyEntity();
$ContentUserDocument = $this->UsersDocuments->patchEntity($ContentUserDocument, $data, [
    'associated' => ['UsersDocumentsItems']
]);

```

Thank you!

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 12, 2020, 10:02pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/2 "2020-05-12T22:02:51Z")

</div>

There is a `toArray()` method available for entities.

`$entityObj->toArray()`

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [May 12, 2020, 10:06pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/3 "2020-05-12T22:06:42Z")

</div>

If applicate to

> $ContentArray[‘users\_documents\_items’]-\>toArray();

it doesnt work… (Call to a member function toArray() on array)

This variable from session looks like this

```
[
	(int) 0 => object(App\Model\Entity\UsersDocumentsItem) {

		'name' => 'test',
		'type' => 'black',
            ... etc
},
	(int) 1 => object(App\Model\Entity\UsersDocumentsItem) {
   ... etc

```

This is works, but get only one item…

`$ContentArray['users_documents_items'][0]->toArray();`

Is it possible to get all the records in the array? No object?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 12, 2020, 10:14pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/4 "2020-05-12T22:14:22Z")

</div>

If you’re trying to do more than one entity:

`$TableObj->patchEntities($array_of_records)`

note the plural ‘Entities’

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [May 12, 2020, 10:20pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/5 "2020-05-12T22:20:13Z")

</div>

Its doesnt works, because the array has nested entity objects. If I insert a clean array without an object, it works … so I need to convert an array with entity objects to a clean array.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [May 12, 2020, 10:30pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/6 "2020-05-12T22:30:11Z")

</div>

The solution to how to do this is as follows:

```
foreach($ContentArray['users_documents_items'] as $arr) {
    $arr2[] = $arr->toArray();
}

```

Is there any other solution to the internal function of phpcake?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 12, 2020, 10:30pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/7 "2020-05-12T22:30:30Z")

</div>

Loop through your containing array and array-ify each object. Then you will have an array of arrays.

Use any php loop structure you want; foreach, for, while… or use Cake’s Collection::map or Collection::reduce.

[https://book.cakephp.org/4/en/core-libraries/collections.html](https://book.cakephp.org/4/en/core-libraries/collections.html)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [May 12, 2020, 10:31pm UTC](https://discourse.cakephp.org/t/entity-object-to-array/7813/8 "2020-05-12T22:31:18Z")

</div>

Thank you very much!
