我有一個(gè)現(xiàn)有的多個(gè)數(shù)組的關(guān)聯(lián)數(shù)組。然后我有一個(gè)“名稱”數(shù)組,我需要在前一個(gè)數(shù)組中找到每個(gè)“名稱”,并通過鍵刪除該數(shù)組。這里的目標(biāo)是獲取現(xiàn)有的文檔數(shù)組,允許用戶“刪除”它們,所以我必須比較用戶想要?jiǎng)h除的文件的名稱,然后在我的父數(shù)組中取消設(shè)置這些數(shù)組中的每一個(gè),然后通過將這個(gè)最終數(shù)組存入數(shù)據(jù)庫(kù)以保存為用戶的“更新”列表。偽代碼將是: - 比較$names每個(gè)名稱的數(shù)組,查找與數(shù)組中的名稱匹配的$parentArray鍵。在 this 中,未設(shè)置的數(shù)組。name$namesforeach我的多個(gè)數(shù)組的關(guān)聯(lián)數(shù)組在這里:Array([0] => PetManagement\\Pet\\Document Object([file:protected] => https://url.com/file_example_MP3_700KB.mp3[name:protected] => file_example_MP3_700KB.mp3[fileType:protected] => audio/mp3[timestamp:protected] => 2019-08-21 08:28:12[authorID:protected] => 2[extension:protected] =>[extensionIcon:protected] =>)[1] => PetManagement\\Pet\\Document Object([file:protected] => https://url.com/file-sample_100kB.docx[name:protected] => file-sample_100kB.docx[fileType:protected] => application/vnd.openxmlformats-officedocument.wordprocessingml.document[timestamp:protected] => 2019-08-21 08:28:12[authorID:protected] => 2[extension:protected] =>[extensionIcon:protected] =>)[2] => PetManagement\\Pet\\Document Object([file:protected] => https://url.com/file-sample_100kB.odt[name:protected] => file-sample_34343kB.odt[fileType:protected] => application/vnd.oasis.opendocument.text[timestamp:protected] => 2019-08-21 08:28:12[authorID:protected] => 2[extension:protected] =>[extensionIcon:protected] =>))我傳入了一個(gè)“名稱”數(shù)組,例如['file-sample_100kB.odt','file_example_MP3_700KB.mp3'],我想搜索這個(gè)大數(shù)組以刪除name鍵與該數(shù)組中的每個(gè)值相匹配的鍵,以及unset鍵。因此,在我的示例中,使用$names數(shù)組,我將刪除 key3和 0.我一直在使用的通用代碼如下,但我似乎無(wú)法讓它正常工作:$docs = $p->getDocuments(); //existing $parentArray$docsToRemove = explode(',',$this->postData['documents']); //create array of names we want to remove from $docsforeach ($docsToRemove as $key=>$value) { //$value is the name we want to remove //'name' is the assoc array key we want to remove error_log(print_r(array_search($value, array_column($docs, 'name'))),true); //returns empty array}任何幫助將不勝感激。在此之前,我是做多foreach環(huán),但使用的混合被告知只是array_search()和array_column()會(huì)是一個(gè)更好更先進(jìn)的解決方案。
在 PHP 中查找該數(shù)組中的值時(shí),通過鍵刪除關(guān)聯(lián)數(shù)組
拉風(fēng)的咖菲貓
2021-12-03 14:56:59