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");
}
}
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
}
});
}
}
0 comments:
Post a Comment