<p>You can use an entry query to retrieve the entries by their IDs and then use <a href="https://www.php.net/manual/en/function.array-map.php" rel="nofollow noreferrer"><code>array_map</code></a> to convert that to an associative array of the properties you want to return for those entries.</p>
<pre class="lang-php prettyprint-override"><code>use craft\elements\Entry;

'transformer' =&gt; function (Entry $entry) {
    $entryData = (object) [];
    $entryData-&gt;pins = $entry-&gt;pins;

    $pinnedEntries = Entry::find()-&gt;id($entryData-&gt;pins)-&gt;status(null)-&gt;all();
    $entryData-&gt;pinnedEntries = array_map(fn (Entry $entry) =&gt; [
        'id' =&gt; $entry-&gt;id,
        'slug' =&gt; $entry-&gt;slug,
        'title' =&gt; $entry-&gt;title,
        'myCustomField' =&gt; $entry-&gt;getFieldValue('myCustomField'),
    ], $pinnedEntries);
    return [
        'title' =&gt; $entry-&gt;title,
        'url' =&gt; $entry-&gt;url,
        'entryData' =&gt; $entryData,
    ];
}
</code></pre>
<p>You can also use an advanced query with a custom <code>select</code> statement and use <code>asArray</code> to get the associative array directly from the query. That's a bit more involved, but will improve performance in case you run into any bottlenecks.</p>

CC BY-SA 4.0
2022-10-05T08:50:23.730Z
2022-10-05T08:50:23.730Z
MoritzLost
<p>I have few entry ids from a plugin(donkeytail), and are arranged in an array,</p>
<p>I need to loop through that array and get title, slug, etc of each entry.</p>
<p>$entry-&gt;pins = [&quot;106&quot;,&quot;122&quot;,&quot;114&quot;,&quot;134&quot;...]</p>
<p>Endpoint:</p>
<pre><code>'single.json' =&gt; function() {
        return [
            'criteria' =&gt; ['section' =&gt; 'home'], 
            'elementType' =&gt; Entry::class,        
            'transformer' =&gt; function(Entry $entry) {
                  $entryData = (object)[];
                  $entryData-&gt;pins = $entry-&gt;pins;
                  $entryData-&gt;pinnedEntries = (object)[];
                  foreach ($entry-&gt;pins as $item) {
                       *Needed Suggestion here to get entry by id ($item)*
                  }
                  return [
                    'title' =&gt; $entry-&gt;title,
                    'url' =&gt; $entry-&gt;url,
                    'entryData' =&gt; $entryData,
                  ];

        }
}
</code></pre>

<craft3><entry><plugin-elementapi>
Element-api get other entries by id inside a entry.json
# Element-api get other entries by id inside a entry.json

(Asked by: username_0 on 2022-10-04)

I have few entry ids from a plugin(donkeytail), and are arranged in an array,
I need to loop through that array and get title, slug, etc of each entry.
$entry->pins = ["106","122","114","134"...]
Endpoint:
'single.json' => function() {
        return [
            'criteria' => ['section' => 'home'], 
            'elementType' => Entry::class,        
            'transformer' => function(Entry $entry) {
                  $entryData = (object)[];
                  $entryData->pins = $entry->pins;
                  $entryData->pinnedEntries = (object)[];
                  foreach ($entry->pins as $item) {
                       *Needed Suggestion here to get entry by id ($item)*
                  }
                  return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'entryData' => $entryData,
                  ];

        }
}

**Tags:** craft3, entry, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2022-10-05. Score: 0 | Accepted answer

You can use an entry query to retrieve the entries by their IDs and then use array_map to convert that to an associative array of the properties you want to return for those entries.
use craft\elements\Entry;

'transformer' => function (Entry $entry) {
    $entryData = (object) [];
    $entryData->pins = $entry->pins;

    $pinnedEntries = Entry::find()->id($entryData->pins)->status(null)->all();
    $entryData->pinnedEntries = array_map(fn (Entry $entry) => [
        'id' => $entry->id,
        'slug' => $entry->slug,
        'title' => $entry->title,
        'myCustomField' => $entry->getFieldValue('myCustomField'),
    ], $pinnedEntries);
    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'entryData' => $entryData,
    ];
}

You can also use an advanced query with a custom select statement and use asArray to get the associative array directly from the query. That's a bit more involved, but will improve performance in case you run into any bottlenecks.


---

