1

How can I delete an array index by comparing it to another array index?

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 )

I can see these two indexes are identical, I am using a for loop but it seeks that php are not seeing them as identical.

How can I compare the array properly and if identical remove the identical array index from the list?

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

3 Answers 3

2

A simple way to check if two keys are present in two separate arrays is using the array_intersect_key() function.

$sameKeys = array_intersect_key($arr1, $arr2);
Sign up to request clarification or add additional context in comments.

2 Comments

I get Fatal error: Call to undefined function array_intersect_keys()
My, bad, spelling mistake, it's array_intersect_key()
0

Having 2 arrays:

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

//You only need this:

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

3 Comments

I get the warning: array_diff(): Argument #1 is not an array
I have made it to work and the function deletes the key. However, if I save it back to json file the format its changed to '{"1":{}}' instead of '[{"",""},{"",""}...]
The problem is if I delete key 1 the array is not up dated and it missing the key 1. "[0] => Array(...)[2] => Array (...)" the new array is missing "[1] => Array(...)"
0

You can use this: array_unique( array_merge($arr1, $arr2) );

OR this:

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

1 Comment

i get Catchable fatal error: Object of class stdClass could not be converted to string in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.