Programmatically set the value of a multi-value field from multiple arrays I have 3 taxonomy vocabularies:


Fruits
Vegetables
Drinks


On the Article content type, I have set a multi-value entity reference field field_article_all_items referencing all of Fruits, Vegetables and Drinks taxonomy vocabularies.

Using the Webform module, I have created a webform with 3 elements:


fruits_category which is a non-required, multi-value Entity Checkboxes element referencing the Fruits Taxonomy Vocabulary
vegetables_category which is a non-required, multi-value Entity Checkboxes element referencing the Vegetables Taxonomy Vocabulary
drinks_category which is a non-required, multi-value Entity Checkboxes element referencing the Drinks Taxonomy Vocabulary


Now on webform submit, I want to:


Create a new node of type article.
Set the values of field_article_all_items equal to all selected values
from fruits_category, vegetable_category and drinks_category.


In my customhandler.php file, I have tried:

// Get an array of the values from the submission.
    $values = $webform_submission-&gt;getData();

// Get 'Fruits Category' Value.
    $FruitsCategory = $values['fruits_category']; // Multi-value tid

// Get 'Vegetables Category' Value.
    $VegetablesCategory = $values['vegetable_category']; // Multi-value tid

// Get 'Drinks Category' Value.
    $DrinksCategory = $values['drinks_category']; // Multi-value tid

// Put all values in an array().
    $AllItems = array($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' =&gt; 'article',        
        'title' =&gt; Test,
        'langcode' =&gt; 'en',
        'uid' =&gt; '1',
        'status' =&gt; '1',
        'created' =&gt; $current_time,
        'field_article_all_items' =&gt; $AllItems,       
        ]);
// Save the created node.
    $article-&gt;save();


the above code created a new node but with field_article_all_items field empty.

Also I have tried:

// Get an array of the values from the submission.
    $values = $webform_submission-&gt;getData();

// Get 'Fruits Category' Value.
    $FruitsCategory = $values['fruits_category']; // Multi-value tid

// Get 'Vegetables Category' Value.
    $VegetablesCategory = $values['vegetable_category']; // Multi-value tid

// Get 'Drinks Category' Value.
    $DrinksCategory = $values['drinks_category']; // Multi-value tid

// Put all values in an array().
    $AllItems = array($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' =&gt; 'article',        
        'title' =&gt; Test,
        'langcode' =&gt; 'en',
        'uid' =&gt; '1',
        'status' =&gt; '1',
        'created' =&gt; $current_time,
        //'field_article_all_items' =&gt; $AllItems,       
        ]);
// Save the created node.
    $article-&gt;save();

foreach ($AllItems as $Item) {
  $article-&gt;field_article_all_items[] = $Item;
}

// Save the created node again.
    $article-&gt;save();

// Also I have tried the below method.
//   $article-&gt;field_article_all_items[] = ['target_id' =&gt; $AllItems]

  //Save the created node again.
//   $article-&gt;save();


But also this code created a new node but with field_article_all_items field empty.

How to set the values of field_article_all_items equal to fruits_category + vegetable_category + drinks_category ?
Educated guess: (The key was to merge the arrays by using array_merge())

// Put all values in an array().
$AllItems = array_merge($FruitsCategory, $DrinksCategory, $DrinksCategory); // use array_merge

foreach ($AllItems as $Item) {
  $article-&gt;field_article_all_items[] = ['target_id' =&gt; $Item];
}

$article-&gt;save();


Also it can be used within the node creation:

// Put all values in an array().
    $AllItems = array_merge($FruitsCategory,$VegetablesCategory,$DrinksCategory);

// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' =&gt; 'article',        
        'title' =&gt; Test,
        'langcode' =&gt; 'en',
        'uid' =&gt; '1',
        'status' =&gt; '1',
        'created' =&gt; $current_time,
        'field_article_all_items' =&gt; $AllItems,  // use array_merge
        ]);
// Save the created node.
    $article-&gt;save();