【问题标题】:php - Compare two array and if an index is identical delete itphp - 比较两个数组,如果索引相同,则删除它
【发布时间】:2015-11-03 19:51:40
【问题描述】:

如何通过将数组索引与另一个数组索引进行比较来删除它?

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

}

【问题讨论】:

    标签: php arrays json compare


    【解决方案1】:

    检查两个单独数组中是否存在两个键的简单方法是使用array_intersect_key() 函数。

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

    【讨论】:

    • 我收到致命错误:调用未定义的函数 array_intersect_keys()
    • 我的,糟糕的,拼写错误,它是array_intersect_key()
    【解决方案2】:

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

    【讨论】:

    • 我收到警告:array_diff(): Argument #1 is not an array
    • 我已经使它工作并且该功能删除了密钥。但是,如果我将其保存回 json 文件,则格式将更改为 '{"1":{}}' 而不是 '[{"",""},{"",""}...]跨度>
    • 问题是如果我删除键 1 数组没有更新并且缺少键 1。"[0] => Array(...)[2] => Array (.. .)" 新数组丢失 "[1] => Array(...)"
    【解决方案3】:

    你可以使用这个:array_unique( array_merge($arr1, $arr2) );

    或者这个:

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

    【讨论】:

    • 我得到可捕获的致命错误:stdClass 类的对象无法转换为字符串
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多