Skip to content
Advertisement

PHP set value from stored array to nested foreach loop in chronological order

I’m having a $data array which holds 4 indexes. What I’m trying to achieve here is to set the values from the $data array to the nested foreach loop but and the end of the execution it’s storing the value of the last index.

<?php
// Some IDs.
$nids = [12, 56, 55, 59, 83];

$data = [
  'Ludo Bagman' => 'Head of the Department of Magical Games and Sports within the Ministry of Magic.',
  'Bathilda Bagshot' => 'Author of A History of Magic, and the great aunt of Gellert Grindelwald.',
  'Katie Bell' => 'Gryffindor Quidditch Chaser one year above Harry Potter. Member of Dumbledore's Army.',
  'Cuthbert Binns' => 'Ghost, History of Magic professor.',
];

foreach ($nids as $nid) {
  $term = Drupal::entityTypeManager()->getStorage('node')->load($nid);
  $paragraphs = $term->get('field_paragraphs')->referencedEntities();

  foreach ($paragraphs as $key => $paragraph) {

    if (in_array($key, [0, 1, 3, 6])) {

      // Here, only the last element from the $data array is stored, i.e. value
      // of `Cuthbert Binns`, whereas each of the above `$key` should take the
      // value from the `$data` array in chronological order.
      foreach ($data as $title) {
        $paragraph->get('field_links')->setValue([
          'title' => $title,
        ]);
      }
      $paragraph->save();
    }
  }
}

In the above code, I’m trying to loop-in to only 4 $paragraph, each of them should have value such as:

$paragraph[0]['field_links']['title']; // Head of the Department of Magical Games and Sports within the Ministry of Magic.
$paragraph[1]['field_links']['title']; // Author of A History of Magic, and the great aunt of Gellert Grindelwald.
$paragraph[3]['field_links']['title']; // Gryffindor Quidditch Chaser one year above Harry Potter. Member of Dumbledore's Army.
$paragraph[6]['field_links']['title']; // Ghost, History of Magic professor.

But it is:

$paragraph[0]['field_links']['title']; // Ghost, History of Magic professor.
$paragraph[1]['field_links']['title']; // Ghost, History of Magic professor.
$paragraph[3]['field_links']['title']; // Ghost, History of Magic professor.
$paragraph[6]['field_links']['title']; // Ghost, History of Magic professor.

Advertisement

Answer

In case you want to set the title to the keys.

$data = [
  'Ludo Bagman' => 'Head of the Department of Magical Games and Sports within the Ministry of Magic.',
  'Bathilda Bagshot' => 'Author of A History of Magic, and the great aunt of Gellert Grindelwald.',
  'Katie Bell' => 'Gryffindor Quidditch Chaser one year above Harry Potter. Member of Dumbledore's Army.',
  'Cuthbert Binns' => 'Ghost, History of Magic professor.',
];
$keys = [0,1,3,6];
$keyedData = array_combine($keys, $data);

// ...

foreach ($paragraphs as $key => $paragraph) {
    if (in_array($key, $keys)) {
        $paragraph->get('field_links')->setValue([
          'title' => $keyedData[$key],
        ]);
        
        $paragraph->save();
    }
}

In case you need the names in the future:

$keyedNames = array_combine($keys, array_keys($data));

references

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement