2015-11-03 80 views
1

如何刪除數組索引通過比較它與另一個數組索引?PHP - 比較兩個數組,如果一個索引是相同的刪除它

PHP:

$results = array (array(
      "Name" => $NameUser, 
      "Surname" => $SurnameUser, 
      "MyComment" => $MyComment, 
      "VideoPath" => $VideoPath, 
      "Reg_Date" => $Reg_Date, 
      "ThumbPath" => $ThumbPath, 
      "UserId" => $UserId 
      )); 
    print_r($results[0]); // Array ([Name] => aaa [Surname] => aaa [MyComment] => aaa [VideoPath] => aaa [Reg_Date] => aaa [ThumbPath] => aaa [UserId] => aaa) 

$JSON_List = file_get_contents('test.json'); 
$arr = json_decode($JSON_List); 
print_r($arr[0]); // stdClass Object ([Name] => aaa [Surname] => aaa [MyComment] => aaa [VideoPath] => aaa [Reg_Date] => aaa [ThumbPath] => aaa [UserId] => aaa) 

我可以看到這兩個指標是相同的,我使用的是for循環,但它試圖在PHP沒有看到他們爲相同。

如何正確比較數組,如果完全相同,從列表中刪除相同的數組索引?

PHP:

foreach ($arr as $index) { 

    $countIndex++; 

    print_r($countIndex); 

    if ($arr[$countIndex - 1] == $results[0]) { 

     print_r("array is identical \n"); 
    }else{ 

     print_r("array is not identical \n"); 
    } 

} 

回答

0

有2列:

$results = array (array(
     "Name" => '$NameUser', 
     "Surname" => '$SurnameUser', 
     "MyComment" => '$MyComment', 
     "VideoPath" => '$VideoPath', 
     "Reg_Date" => '$Reg_Date', 
     "ThumbPath" => '$ThumbPath', 
     "UserId" => '$UserId' 
    ) 
); 
// print_r($results[0]); 
//$JSON_List = file_get_contents('test.json'); 
//$arr = json_decode($JSON_List); 
$arr = array(array(//simulate 
     "Name" => '$NameUser', 
     "Surname" => '$SurnameUser', 
     "MyComment" => '$MyComment', 
     "VideoPath" => '$VideoPath', 
     "Reg_Date" => '$Reg_Date', 
     "ThumbPath" => '$ThumbPath', 
     "UserId" => '$UserId' 
    ), 
    array(
     "Name" => '$NameUser2', 
     "Surname" => '$SurnameUser2', 
     "MyComment" => '$MyComment2', 
     "VideoPath" => '$VideoPath2', 
     "Reg_Date" => '$Reg_Date2', 
     "ThumbPath" => '$ThumbPath', 
     "UserId" => '$UserId2' 
)); 

//Search identicals 
function search_identicals(&$arr,$results){ 
    $response = array(); 
    foreach($results as $k=>$value){ 
     array_walk($arr,function($elem,$key)use($value,&$response,&$arr){ 
      $resp = array_diff($elem,$value); 
      if(empty($resp)){ 
       unset($arr[$key]); 
       array_push($response,$elem); 
      } 
     }); 
    } 
    return ($response) ? $response : false; 
} 

$identicals = search_identicals($arr,$results); 
var_dump('to delete'); 
var_dump($identicals); 
var_dump('deleted'); 
var_dump($arr); 

//你只需要這樣:

function search_identicals(&$arr,$results){//&$arr by reference 
    foreach($results as $k=>$value){ 
     array_walk($arr,function($elem,$key)use($value,&$arr){ 
      $resp = array_diff($elem,$value);//if is identical, return empty 
      if(empty($resp)){ 
       unset($arr[$key]);//remove repeated 
      } 
     }); 
    } 
} 
+0

我得到的警告:array_diff():參數#1不是陣列y – SNos

+0

我已經使其工作並且該功能刪除了密鑰。但是,如果我將它保存回json文件,則其格式會更改爲「{」1「:{}}」,而不是「[{」「,」「},{」「,」「} ...] – SNos

+0

問題是如果我刪除密鑰1數組未更新並且缺少密鑰1.「[0] => Array(...)[2] => Array(...)」新數組缺失「 [1] => Array(...)「 – SNos

0

您可以使用此:array_unique(array_merge($arr1, $arr2));

OR這樣的:

$arr_1 = array_diff($arr1, $arr2); 
$arr_2 = array_diff($arr2, $arr1); 
+0

我得到捕致命錯誤:類stdClass的客體不能在 – SNos

2

一個簡單的方法來檢查,如果兩個鍵存在於兩個不同的陣列是使用array_intersect_key()功能。

$sameKeys = array_intersect_key($arr1, $arr2); 
+0

我轉換爲字符串得到致命錯誤:調用未定義的函數array_intersect_keys() – SNos

+0

我的,壞的,拼寫錯誤,它的array_intersect_key() – Pavlin

相關問題