How do I access a node object property, if it is an array? $node = \Drupal\node\Entity\Node::load($nid);


Having loaded a node object in Drupal 8, I know how to access a property, if it is a single value, and assign it to a variable, $test in the example below:

$test = $node-&gt;field_1-&gt;value;


However, if $node-&gt;field_1 contains an array, how do I access it so that I can assign it to $test? So $test would equal something like:

array(0 =&gt; 'apples', 1 =&gt; 'oranges')
There's no real need to convert it to an array, as you can already access members of a FieldItemList as an array:

$first = $node-&gt;field_1[0]-&gt;value;
$next = $node-&gt;field_1[1]-&gt;value;


This:

$node-&gt;field_1-&gt;value;


Is just a shortcut to the first item in the collection anyway (see FieldItemList::__get()).

If you need the values in exactly the format you specified, FieldItemList is also iterable:

foreach ($node-&gt;field_1 as $delta =&gt; $item) {
  $an_array[$delta] = $item-&gt;value;
}