# Displaying array data from REST API

**URL:** <https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227>\
**Category:** Questions\
**Tags:** v3\
**Created:** [August 17, 2022, 11:43am UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227 "2022-08-17T11:43:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RayGun](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/raygun/32/12254_2.png) [@RayGun](https://forum.getkirby.com/u/RayGun)\
**Post date:** [August 17, 2022, 11:43am UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/1 "2022-08-17T11:43:29Z")

</div>

Kirby-newbie here, trying to get array data from an API to virtual pages.

I followed the setup [here](https://getkirby.com/docs/guide/virtual-pages/content-from-api) and everything is working fine apart from the info that is in array format.

The source is in JSON and it’s something like this:

```auto
[{"lastname":"Doe","firstname":"John","roles":["senior","developer","staff"]}]

```

I can get the array into a field in the page model, but after that, I’m stumped. I have tried to go through the array data using _for_ and _foreach_, but I get an error about “Array to string conversion”. Using something like _$page-\>roles()-\>toString()_ does the same thing where as _$page-\>roles()-\>get()_ doesn’t return anything. Yup, I have been trying a lot of random stuff already 😉

If I use _dump($page-\>roles())_, the contents is there, but how can I get the info out in plain “value1, value2, value3” 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:** [August 17, 2022, 12:00pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/2 "2022-08-17T12:00:30Z")

</div>

> [@RayGun](#):
>
> I can get the array into a field in the page model, but after that, I’m stumped.

Hey, and welcome the forum. Must admit that I don’t quite understand. How did you assign the fields from the API to your page model? Could you please post the model?

---

<div class="post-metadata">

**Author:** ![RayGun](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/raygun/32/12254_2.png) [@RayGun](https://forum.getkirby.com/u/RayGun)\
**Post date:** [August 17, 2022, 12:05pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/3 "2022-08-17T12:05:56Z")

</div>

```auto
<?php

class OsaajatPage extends Page
{
    public function children()
    {
        $results = [];
        $pages = [];
        $request = Remote::get('url-to-source');

        if ($request->code() === 200) {
            $results = $request->json(true);
        }

        foreach ($results as $key => $osaaja) {
            $pages[] = [
                'slug' => Str::slug($osaaja['lastname'].'-'.$osaaja['firstname']),
                'num' => $key+1,
                'template' => 'osaaja',
                'model' => 'osaaja',
                'content' => [
                    'title' => $osaaja['lastname'].' '.$osaaja['firstname'],
					'roles' => $osaaja['roles']
                ]
            ];
        }

        return Pages::factory($pages, $this);
    }
}

```

Here I can get a single value to a field if I use the key, e.g. _‘roles’ =\> $osaaja[‘roles’][‘0’]_. I can’t use that in the page template, though.

---

<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:** [August 17, 2022, 1:42pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/4 "2022-08-17T13:42:27Z")

</div>

> [@RayGun](#):
>
> `$osaaja['roles']`

Ok, since that’s an array, and you probably want to store it in a multiselect field as a comma separated list, you have to implode it:

```auto
'roles' => implode(', ', $osaaja['roles']),

```

---

<div class="post-metadata">

**Author:** ![RayGun](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/raygun/32/12254_2.png) [@RayGun](https://forum.getkirby.com/u/RayGun)\
**Post date:** [August 17, 2022, 2:25pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/5 "2022-08-17T14:25:04Z")

</div>

That works, yay!

A follow-up to this: there are e.g. address fields that are stored as arrays as well. Street address, post code and city are separate fields, also stored as arrays because one person can have several addresses in the source info.

I was going to make a for/foreach loop to go through the data in the fields, but is there a quicker or a more elegant way to do this?

---

<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:** [August 17, 2022, 2:40pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/6 "2022-08-17T14:40:52Z")

</div>

Need to see the raw data. And you have to store this in a structure field (which needs yaml encoded data)

---

<div class="post-metadata">

**Author:** ![RayGun](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/raygun/32/12254_2.png) [@RayGun](https://forum.getkirby.com/u/RayGun)\
**Post date:** [August 17, 2022, 4:34pm UTC](https://forum.getkirby.com/t/displaying-array-data-from-rest-api/26227/7 "2022-08-17T16:34:36Z")

</div>

It’s ok, I got it working using _explode_ and _for_ clauses. Not the prettiest, but works in this case.

Thank you for your help!
