# Get field from array and work like normal

**URL:** <https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 23, 2019, 7:29pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030 "2019-02-23T19:29:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 23, 2019, 7:29pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/1 "2019-02-23T19:29:34Z")

</div>

I have the name of a field in an array and I want to turn it back into a field i can work with.

This wont work obviously because its a string now:

```auto
$att['field']->toDate($format)

```

How can i turn `$att['field']` into something i can then chain stuff onto like I would directly on a field?

---

<div class="post-metadata">

**Author:** ![moeli](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/moeli/32/8964_2.png) [@moeli](https://forum.getkirby.com/u/moeli)\
**Post date:** [February 23, 2019, 8:51pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/2 "2019-02-23T20:51:11Z")

</div>

I don’t think you can “turn” a string into a field but you should be able to call it by name.  
Assuming you are in a page context (untested):

```auto
if($field = $page->$att['field']()->exists()) {
   echo $field->toDate($format);
}

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 23, 2019, 8:54pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/3 "2019-02-23T20:54:06Z")

</div>

Thanks for trying, but it doesnt work, it still sees it as a string. I’m doing this in a plugin.

---

<div class="post-metadata">

**Author:** ![moeli](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/moeli/32/8964_2.png) [@moeli](https://forum.getkirby.com/u/moeli)\
**Post date:** [February 23, 2019, 9:03pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/4 "2019-02-23T21:03:48Z")

</div>

It is difficult to find out what the problem is without more information like error messages and your code.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 23, 2019, 9:06pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/5 "2019-02-23T21:06:25Z")

</div>

It’s a big plugin, to much to post, but the problem really is just that - I know the name of the field in an array and i want get it as a proper field so i can then chain field methods on to it, if the field exists.

I got the following error with the code you posted…

```auto
**Fatal error** : Uncaught Whoops\Exception\ErrorException: Array to string conversion

```

I did this in a function…

```auto
$att = json_decode(json_encode($tag['attributes']), true);

if($field = page()->$att['field']()->exists()) {
	return $field->toDate($format);
}

```

---

<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:** [February 23, 2019, 9:17pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/6 "2019-02-23T21:17:06Z")

</div>

> [@jimbobrjames](#):
>
> if($field = page()-\>$att’field’-\>exists()) { return $field-\>toDate($format); }

This doesn’t make sense because `$field` will now be a boolean, not a field object.

But

```auto
if(page()->{$att['field']}()->exists()) {
	return page()->{$att['field']}()->toDate($format);
}

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 23, 2019, 9:20pm UTC](https://forum.getkirby.com/t/get-field-from-array-and-work-like-normal/13030/7 "2019-02-23T21:20:09Z")

</div>

Thanks 🙂 That did the trick… expect an awesome plugin sometime soon 🙂
