Answers.Body: <p>you could use <a href="http://php.net/manual/en/function.array-values.php" rel="nofollow noreferrer"><code>array_values()</code></a> to fetch the first image:</p>
  
  <pre><code>...
        'transformer' =&gt; function (Entry $entry) {
          foreach ($entry-&gt;images as $image) {
            $images[] = $image-&gt;url;
          }
          return [
            'images' =&gt;  array_values($images)[0],
          ];
        },
  ...
  </code></pre>
  
  <p>or use <a href="http://php.net/manual/en/function.reset.php" rel="nofollow noreferrer"><code>reset()</code></a></p>
  
  <pre><code>...
        'transformer' =&gt; function (Entry $entry) {
          foreach ($entry-&gt;images as $image) {
            $images[] = $image-&gt;url;
          }
          return [
            'images' =&gt;  reset($images),
          ];
        },
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-13T10:27:53.360Z
Answers.Text: Thanks a lot! `reset()` works fine. When I use `array_values()` I get the following error: `500 (Undefined offset: 0)`. When I dump the entire array, the key `0` seems to be present. It works with `reset()` so I'm fine, but I'm curious why the other solution doesn't work.
Answers.UserDisplayName: Anthronaut
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-11T17:18:48.067Z
Answers.LastActivityDate: 2018-09-11T17:23:52.263Z
Answers.LastEditDate: 2018-09-11T17:23:52.263Z
Answers.UserDisplayName: HendrikEng
Answers.Body: <p>I was wondering this myself, and I figured there had to be an easier built-in Craft solution rather than using pure PHP like HendrikEng suggested, and it turns out there is! Just use <code>$entry-&gt;assetsFieldHandle-&gt;one()-&gt;url</code>:</p>
  <pre><code>use craft\elements\Entry;
  
  return [
      'endpoints' =&gt; [
          'entries.json' =&gt; function() {
              return [
                  'elementType' =&gt; Entry::class,
                  'elementsPerPage' =&gt; 10,
                  'criteria' =&gt; [
                      'section' =&gt; 'blog'
                  ],
                  'transformer' =&gt; function(Entry $entry) {
                      return [
                          'id' =&gt; $entry-&gt;id,
                          'title' =&gt; $entry-&gt;title,
                          'url' =&gt; $entry-&gt;url,
                          'mainImg' =&gt; $entry-&gt;mainImg-&gt;one()-&gt;url
                      ];
                  }
              ];
          }
      ]
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-21T19:41:43.277Z
Answers.LastActivityDate: 2023-10-21T19:41:43.277Z
Answers.UserDisplayName: kmgdev
Answers.Body: <p>You can do something like this:</p>
  <pre><code>'transformer' =&gt; function(craft\elements\Entry $entry) {
      $image = $entry-&gt;photos-&gt;one();
  
      return [
          'image' =&gt; $image ? $image-&gt;url : null,
      ];
  },
  </code></pre>
  <p><strong>More info <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">here.</a></strong></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-01T17:36:18.127Z
Answers.LastActivityDate: 2021-03-01T18:46:24.567Z
Answers.LastEditDate: 2021-03-01T18:46:24.567Z
Answers.UserDisplayName: Alexis Dalai Waldo Jiménez
Body: <p>I have an asset field in my current project with the handle <code>postImage</code>, containing multiple image files. In my element-api endpoint, I wish to access the url of the first file only.</p>
  
  <p>I tried the following code:</p>
  
  <pre><code>$imgs = $entry-&gt;postImage;
  $imgUrl = $imgs[0]-&gt;getUrl();
  </code></pre>
  
  <p>The resulting console error message says:</p>
  
  <pre><code>500 (Call to a member function getUrl() on string)
  </code></pre>
  
  <p>Looping through the array works fine, but I don't want all image urls, just the first one.</p>
  
  <p>It's probably very easy, but that ties a knot my brain. I don't understand where the array is being converted to a string, can anyone hint me to the solution?</p>
  
  <p>Thanks a lot!</p>
  
Tags: <url><php><plugin-elementapi><array>
Title: Element-API: Access the first url of an asset array
ThreadText: # Element-API: Access the first url of an asset array
  
  (Asked by: username_0 on 2018-09-11)
  
  I have an asset field in my current project with the handle `postImage`, containing multiple image files. In my element-api endpoint, I wish to access the url of the first file only.
  
  I tried the following code:
  
  ```
  $imgs = $entry->postImage;
  $imgUrl = $imgs[0]->getUrl();
  
  ```
  
  The resulting console error message says:
  
  ```
  500 (Call to a member function getUrl() on string)
  
  ```
  
  Looping through the array works fine, but I don't want all image urls, just the first one.
  
  It's probably very easy, but that ties a knot my brain. I don't understand where the array is being converted to a string, can anyone hint me to the solution?
  
  Thanks a lot!
  
  **Tags:** url, php, plugin-elementapi, array
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2018-09-11. Score: 2 | Accepted answer
  
  you could use [`array_values()`](http://php.net/manual/en/function.array-values.php) to fetch the first image:
  
  ```
  ...
        'transformer' => function (Entry $entry) {
          foreach ($entry->images as $image) {
            $images[] = $image->url;
          }
          return [
            'images' =>  array_values($images)[0],
          ];
        },
  ...
  
  ```
  
  or use [`reset()`](http://php.net/manual/en/function.reset.php)
  
  ```
  ...
        'transformer' => function (Entry $entry) {
          foreach ($entry->images as $image) {
            $images[] = $image->url;
          }
          return [
            'images' =>  reset($images),
          ];
        },
  ...
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks a lot! `reset()` works fine. When I use `array_values()` I get the following error: `500 (Undefined offset: 0)`. When I dump the entire array, the key `0` seems to be present. It works with `reset()` so I'm fine, but I'm curious why the other solution doesn't work.  
  
  ---
  
  ### Answer by username_2 on 2023-10-21. Score: 1
  
  I was wondering this myself, and I figured there had to be an easier built-in Craft solution rather than using pure PHP like username_1 suggested, and it turns out there is! Just use `$entry->assetsFieldHandle->one()->url`:
  
  ```
  use craft\elements\Entry;
  
  return [
      'endpoints' => [
          'entries.json' => function() {
              return [
                  'elementType' => Entry::class,
                  'elementsPerPage' => 10,
                  'criteria' => [
                      'section' => 'blog'
                  ],
                  'transformer' => function(Entry $entry) {
                      return [
                          'id' => $entry->id,
                          'title' => $entry->title,
                          'url' => $entry->url,
                          'mainImg' => $entry->mainImg->one()->url
                      ];
                  }
              ];
          }
      ]
  ];
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2021-03-01. Score: 1
  
  You can do something like this:
  
  ```
  'transformer' => function(craft\elements\Entry $entry) {
      $image = $entry->photos->one();
  
      return [
          'image' => $image ? $image->url : null,
      ];
  },
  
  ```
  
  **More info [here.](https://github.com/craftcms/element-api)**
  
  
  ---
  
  