簡體   English   中英

根據同一數組中的另一個唯一值將一組值推入數組

[英]Push a set of values to an array based on another unique value in the same array

問題背景

您好,我有以下電影攝制組成員:

array:7 [▼
  0 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5e9d"
    "department" => "Directing"
    "id" => 139098
    "job" => "Director"
    "name" => "Derek Cianfrance"
    "profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
  ]
  1 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5ed7"
    "department" => "Writing"
    "id" => 139098
    "job" => "Story"
    "name" => "Derek Cianfrance"
    "profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
  ]
  2 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5edd"
    "department" => "Writing"
    "id" => 132973
    "job" => "Story"
    "name" => "Ben Coccio"
    "profile_path" => null
  ]
  3 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5ee3"
    "department" => "Writing"
    "id" => 139098
    "job" => "Screenplay"
    "name" => "Derek Cianfrance"
    "profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
  ]
  4 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5ee9"
    "department" => "Writing"
    "id" => 132973
    "job" => "Screenplay"
    "name" => "Ben Coccio"
    "profile_path" => null
  ]
  5 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5eef"
    "department" => "Writing"
    "id" => 1076793
    "job" => "Screenplay"
    "name" => "Darius Marder"
    "profile_path" => null
  ]
  11 => array:6 [▼
    "credit_id" => "52fe49de9251416c750d5f13"
    "department" => "Camera"
    "id" => 54926
    "job" => "Director of Photography"
    "name" => "Sean Bobbitt"
    "profile_path" => null
  ]
]

如您所見,這是我通過TMDb API獲得的積分列表。 構建上述數組的第一步是過濾掉我不想顯示的所有作業,這是我的操作方法:

$jobs = [ 'Director', 'Director of Photography', 'Cinematography', 'Cinematographer', 'Story', 'Short Story', 'Screenplay', 'Writer' ];

$crew = array_filter($tmdbApi, function ($crew) use ($jobs) {
    return array_intersect($jobs, $crew);
});

我的問題

我想弄清楚如何使上述結果更進一步,並合並id相同的jobs ,從而得到這樣的結果,例如:

array:7 [▼
  0 => array:6 [▼
    "credit_id" => "52fe49dd9251416c750d5e9d"
    "department" => "Directing"
    "id" => 139098
    "job" => "Director, Story, Screenplay"
    "name" => "Derek Cianfrance"
    "profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
  ]

我也考慮過按照我的邏輯去做,而不是在我的刀片模板中去做,但是我不確定如何實現。

您將如何實現?

由於您正在嘗試編輯數組元素及其大小,因此我相信array_map()array_filter()將不是解決方案。

這就是我能想到的...

$jobs = [
  'Director', 'Director of Photography', 'Cinematography',
  'Cinematographer', 'Story', 'Short Story', 'Screenplay', 'Writer'
];

$crew = [];

foreach($tmdbApi as $key => $member) {
  if($member['id'] == $id && in_array($member['job'], $jobs)) {
    if(!isset($crew[$key]))  {
      $crew[$key] = $member;
    } else {
      $crew_jobs = explode(', ', $crew[$key]['job']);
      if(!in_array($member['job'], $crew_jobs)) {
        $crew_jobs[] = $member['job'];
      }
      $crew[$key]['job'] = implode(', ', $crew_jobs);
    }
  }
}

希望這能回答您的問題:)

在這種情況下,您可以很好地使用Laravel的Collection,它有很多方法可以在這種情況下為您提供幫助。

首先,將此數組(您已經在作業中過濾的數組)轉到集合:

$collection = collect($crew);

其次,按id這個Collection分組:

$collectionById = $collection->groupBy('id');

現在,將結果 id 分組,並轉換為一個集合,其中對應於id,其值是一個“匹配”結果數組。 有關此的更多信息。

最后,只是一個簡單的腳本,它遍歷每個id所有結果並組合job字段:

$combinedJobCollection = $collectionById->map(function($item) {
    // get the default object, in which all fields match
    // all the other fields with same ID, except for 'job'
    $transformedItem = $item->first();

    // set the 'job' field according all the (unique) job
    // values of this item, and implode with ', '
    $transformedItem['job'] = $item->unique('job')->implode('job', ', ');

    /* or, keep the jobs as an array, so blade can figure out how to output these
    $transformedItem['job'] = $item->unique('job')->pluck('job');
    */

    return $transformedItem;
})->values();
// values() makes sure keys are reordered (as groupBy sets the id
// as the key)

此時,將返回此Collection:

Collection {#151 ▼
  #items: array:4 [▼
    0 => array:6 [▼
      "credit_id" => "52fe49dd9251416c750d5e9d"
      "department" => "Directing"
      "id" => 139098
      "job" => "Director, Story, Screenplay"
      "name" => "Derek Cianfrance"
      "profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
    ]
    1 => array:6 [▼
      "credit_id" => "52fe49dd9251416c750d5edd"
      "department" => "Writing"
      "id" => 132973
      "job" => "Story, Screenplay"
      "name" => "Ben Coccio"
      "profile_path" => null
    ]
    2 => array:6 [▼
      "credit_id" => "52fe49dd9251416c750d5eef"
      "department" => "Writing"
      "id" => 1076793
      "job" => "Screenplay"
      "name" => "Darius Marder"
      "profile_path" => null
    ]
    3 => array:6 [▼
      "credit_id" => "52fe49de9251416c750d5f13"
      "department" => "Camera"
      "id" => 54926
      "job" => "Director of Photography"
      "name" => "Sean Bobbitt"
      "profile_path" => null
    ]
  ]
}

注意:將此Collection用作數組,請使用:

$crew = $combinedJobCollection->toArray();

有多種方法可以實現此目的,例如:在數組中search重疊的ID,但是我認為這是實現此目的的最簡單方法。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM