2016-11-26 24 views
0

問題背景

您好,我有電影劇組成員的以下數組:推一組值基於相同的陣列中的另一個獨特的價值數組

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); 
}); 

我的問題

我想弄清楚如何採取上述結果又進了一步,並結合jobs其中id是一樣的,所以與這樣的事情結束了,例如:

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

我也考慮過迫降在我的邏輯這樣做的,而不是在我的刀片上做模板,但我不知道如何實現這一點。

你會怎麼做到這一點?

回答

1

你可以很好地使用Laravel的收藏在這種情況下,它有一個很大的數目方法,這將有助於你在這種情況下。

首先,把這個陣列(一個你已經過濾的作業)將Collection:

$collection = collect($crew); 

二組此係列由它的id S:

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

現在,結果分組id並轉換爲一個集合,其中對應於該id,並且該值爲'匹配'結果數組。更多關於它的信息here

最後,只是一個簡單的腳本,通過所有的結果對每個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 {#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的數組,但我認爲這是實現此目的的最簡單方法。

Goodluck!

+0

哇!這非常好,非常感謝,我還不熟悉Laravel的collection()功能。這真太了不起了。 – gmask

+0

是的。你可能會考慮去除鏈式「implode」方法,並用'pluck('job')'替換它。這樣數據將保持爲一個數組,並且您可以選擇如何在刀片模板中輸出這些作業,所以也可以 - 例如 - 將作業輸出到某種列表中,而不是用逗號分隔的靜態字符串。顯示數據是當然Blade的工作。 –

+0

我已將此添加到我的答案中。 –

1

由於您試圖編輯數組元素及其大小,我相信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); 
    } 
    } 
} 

希望這回答你的問題:)

相關問題