$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->field_1->value;

However, if $node->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 => 'apples', 1 => 'oranges')

1 Answer 1


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->field_1[0]->value;
$next = $node->field_1[1]->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->field_1 as $delta => $item) {
  $an_array[$delta] = $item->value;
  • Great that worked. Out of interest, why do you use $delta => $item, rather than $key => $value? Just personal preference, or is this the correct language to use when dealing with objects, rather than arrays?
    – dbj44
    Feb 3, 2015 at 10:37
  • It just better represents what those vars are in this context than key+value. Makes the code a bit easier to read
    – Clive
    Feb 3, 2015 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.