<p>I dont think you can build arrays in PHP that way and you end up passing <code>array(0 =&gt; '1021,1047,1076,1123')</code> to the param.</p>

<p>Not shure if passing the string is supported:</p>

<pre><code>'article_countries' =&gt; $article_countries
</code></pre>

<p>If not you could build the array like so:</p>

<pre><code>'article_countries' =&gt; array_map('trim', explode(',', $article_countries))
</code></pre>

CC BY-SA 3.0
2015-08-24T14:40:17.307Z
Thanks, your comment was helpful for getting me on the right track this AM
kestonh
CC BY-SA 3.0
2015-08-22T14:29:08.250Z
2015-08-22T14:29:08.250Z
carlcs
<p>Thanks carlcs, your comment &amp; fresh eyes set me on the right track this morning.</p>

<p>Not real sure why I was wrapping the $article_countries var in another array...</p>

<p>Hopefully this will help somebody else out at 5PM on a Friday...</p>

CC BY-SA 3.0
2015-08-24T14:42:25.570Z
2015-08-24T14:42:25.570Z
kestonh
<p>This may just be a 5PM Friday issue, but I'm scratching my head over here. I'm trying to associate a string of related entry id's represented by a variable with a new entry via the API. When I pass the variable, only the first one gets inserted into the DB. If I use the string they all get inserted. Here's the insert code:</p>

<pre><code> $article_countries = "1021,1047,1076,1123"; 

 $entry-&gt;getContent()-&gt;setAttributes(array(
   'article_countries' =&gt; array(
     $articleCountries
    )
 ));
</code></pre>

<p>Why is the string working where the variable isn't?</p>

<plugin-development><relations><elementcriteriamodel>
Associating related entries to a new entry when using the API
# Associating related entries to a new entry when using the API

(Asked by: username_0 on 2015-08-21)

This may just be a 5PM Friday issue, but I'm scratching my head over here. I'm trying to associate a string of related entry id's represented by a variable with a new entry via the API. When I pass the variable, only the first one gets inserted into the DB. If I use the string they all get inserted. Here's the insert code:
 $article_countries = "1021,1047,1076,1123"; 

 $entry->getContent()->setAttributes(array(
   'article_countries' => array(
     $articleCountries
    )
 ));

Why is the string working where the variable isn't?

**Tags:** plugin-development, relations, elementcriteriamodel


---

## 2 answers

### Answer by username_1 on 2015-08-22. Score: 1

I dont think you can build arrays in PHP that way and you end up passing array(0 => '1021,1047,1076,1123') to the param.
Not shure if passing the string is supported:
'article_countries' => $article_countries

If not you could build the array like so:
'article_countries' => array_map('trim', explode(',', $article_countries))

> **Comments:**
> **username_0**: Thanks, your comment was helpful for getting me on the right track this AM  

---

### Answer by username_0 on 2015-08-24. Score: 0

Thanks username_1, your comment & fresh eyes set me on the right track this morning.

Not real sure why I was wrapping the $article\_countries var in another array...

Hopefully this will help somebody else out at 5PM on a Friday...


---

