# Add to structure

**URL:** <https://forum.getkirby.com/t/add-to-structure/21591>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 13, 2021, 5:38pm UTC](https://forum.getkirby.com/t/add-to-structure/21591 "2021-03-13T17:38:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 13, 2021, 5:38pm UTC](https://forum.getkirby.com/t/add-to-structure/21591/1 "2021-03-13T17:38:02Z")

</div>

I always use the popular addToStructure() function from Sonja and it’s working great. However, I have seen that there are a lot of other structure methods. Can I also work with them instead of the custom function? Has anybody a simple example how to add a entry to a structure in PHP with this methods?

I always use:

```
function addToStructure($page, $field, $data = array()){
		$fieldData = page($page)->$field()->yaml();
		$fieldData[] = $data;
		$fieldData = yaml::encode($fieldData);
		try {
			page($page)->update(array($field => $fieldData));
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

```

Can this replaced with: [$structure-\>add() | Kirby](https://getkirby.com/docs/reference/@/classes/cms/structure/add) and maybe how?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 13, 2021, 6:11pm UTC](https://forum.getkirby.com/t/add-to-structure/21591/2 "2021-03-13T18:11:53Z")

</div>

You could, but you would have to create a new StructureObject, then add that to the structure collection, then use `toArray()` to convert the collection to an array and Data::encode that-

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 15, 2021, 11:54am UTC](https://forum.getkirby.com/t/add-to-structure/21591/3 "2021-03-15T11:54:08Z")

</div>

Ah ok, so I think it’s better to simply use your awesome function! BTW: maybe a cookbook article for future how to deal with structure fields data (add, remove, append, prepend, findBy, etc.) 😉
