亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 按時間順序將存儲數組中的值設置為嵌套 foreach 循環

PHP 按時間順序將存儲數組中的值設置為嵌套 foreach 循環

PHP
忽然笑 2023-09-22 17:47:49
我有一個$data包含 4 個索引的數組。我在這里試圖實現的是將$data數組中的值設置為嵌套foreach循環,但在執行結束時它存儲最后一個索引的值。<?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();    }  }}在上面的代碼中,我嘗試只循環到 4 $paragraph,它們中的每一個都應該具有諸如以下的值:$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.但它是:$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.
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

如果您想將title設為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();

    }

}

如果您將來需要這些名稱:


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


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號