我想從基於值的多維數組中刪除元素。在這種情況下,如果一個子數組的鍵'year'的值爲2011,那麼我需要這個子數組。從基於值的多維數組中刪除元素
只是爲了記錄:我運行PHP 5.2。
我的數組是這樣的:
Array
(
[0] => Array
(
[filmId] => 61359
[url] => http://www.moviemeter.nl/film/61359
[title] => Unstoppable
[alternative_title] =>
[year] => 2011
[thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/61000/61359.jpg
[average] => 0
[votes_count] => 0
[similarity] => 100.00
[directors_text] => geregisseerd door Richard Harrison
[actors_text] => met Chen Shilony, Ruben Crow en David Powell
[genres_text] => Drama/Komedie
[duration] => 90
)
[1] => Array
(
[filmId] => 87923
[url] => http://www.moviemeter.nl/film/87923
[title] => Unstoppable
[alternative_title] =>
[year] => 2011
[thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/87000/87923.jpg
[average] => 0
[votes_count] => 0
[similarity] => 100.00
[directors_text] => geregisseerd door Example Director
[actors_text] => met Actor 1, Actor 2 en Actor 3
[genres_text] => Drama/Komedie
[duration] => 90
)
[2] => Array
(
[filmId] => 68593
[url] => http://www.moviemeter.nl/film/68593
[title] => Unstoppable
[alternative_title] =>
[year] => 2010
[thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/68000/68593.jpg
[average] => 3.3
[votes_count] => 191
[similarity] => 100.00
[directors_text] => geregisseerd door Tony Scott
[actors_text] => met Denzel Washington, Chris Pine en Rosario Dawson
[genres_text] => Actie/Thriller
[duration] => 98
)
[3] => Array
(
[filmId] => 17931
[url] => http://www.moviemeter.nl/film/17931
[title] => Unstoppable
[alternative_title] => Nine Lives
[year] => 2004
[thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/17000/17931.jpg
[average] => 2.64
[votes_count] => 237
[similarity] => 100.00
[directors_text] => geregisseerd door David Carson
[actors_text] => met Wesley Snipes, Jacqueline Obradors en Mark Sheppard
[genres_text] => Actie/Thriller
[duration] => 96
)
)
我第二次選擇這個。儘管Jacob解決方案非常優雅,但如果數組的嵌套級別事先已知,那麼我不會打擾使用遞歸,如上例所示。 當然,如果事先不知道數組的結構,則必須依賴遞歸(或array_map與in_array或類似函數的組合,或甚至更快一些)。 – maraspin 2010-12-16 23:53:43
感謝您的回覆!奇妙地工作! – Bob 2010-12-17 14:28:10
就在旁註:我無法得到我想要的結果。不知道我做錯了什麼,但我把它固定在我的情況。我不得不添加「return $ array;」在foreach循環之後,並且像這樣調用它: $ array = removeElementWithValue($ array,「year」,2011); 對於將來使用此功能並希望重新排列索引的用戶,可以使用以下內容: $ array = array_values($ array); – Bob 2010-12-17 18:17:09