Answer the question
In order to leave comments, you need to log in
How to get elements of one array that are missing in another?
I'm trying to compare 2 arrays whose keys are named differently, but I need to compare their values.
The problem is that in the first array these values are repeated many times.
First array:
$arr= [ [ "user_id" => "100",
"Цена" => 100,
"Количество" => 15
],
[ "user_id" => "100",
"Цена" => 60,
"Количество" => 25,
],
[ "user_id" => "100",
"Цена" => 180,
"Количество" => 7
]
];
$arr2= [ [ "users_id" => "100",
"Цена" => 100,
"Количество" => 15
],
[ "users_id" => "101",
"Цена" => 60,
"Количество" => 25,
],
[ "users_id" => "102",
"Цена" => 180,
"Количество" => 7
]
];
$result = [
[ "users_id" => "101",
"Цена" => 60,
"Количество" => 25,
],
[ "users_id" => "102",
"Цена" => 180,
"Количество" => 7
]
];
function array_diff_unic($array1, $array2) {
$result=[];
foreach($array1 as $key => $value) {
foreach($array2 as $key2 => $value2) {
if( $array1['user_id'] != $array2['users_id'] ) {
$result[$key] = $value2;
}
}
}
return $result;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question