'How to compare arrays and get matching data
I have an array $data, that looks something like this:
[
1 => "1234,10-12-2022",
2 => "1356,01-02-2021",
3 => "1677,03-05-2020",
];
Then, I have another array $search that looks like this: ['1234','1677']
I can get results of any of those items in $data by date,but once I have a match, i need to check if there is a match from another array, and get that date[1] and value[0] as output. Also it is possible that in match all 3 values[0] are present.
How I can find most recent matches of it?
This is what I have tried:
`$f = array_values(array_intersect(array_map(
function($item){
return explode(',', $item)[0];
},
$c), $p));
var_dump($f);//finds match
if($f){
//pass this to find date-time but I need most recent one for //every value
$c = get_comment_meta($user_id,'value');
$new_to_old = array_reverse($c);
$newArr = array();
foreach ( $new_to_old as $x ) {
$all_course_meta = explode(",", $x);
array_push ( $newArr, $all_course_meta[0] );
}
$uniqueArr = array_unique($newArr);
$result = array_intersect($uniqueArr, $f);}
var_dump($result);//doeas not returns anything here.`
I have tried various loops and array intersect, diff and etc, but without success.
Solution 1:[1]
I'm not sure if I understand your question here, but if you want to compare the first part of the values in the $data with the values in the $search then I think it's will be like this
$a = ['1234,10-12-2022', '1356,01-02-2021', '1677,03-05-2020'];
$b = ['1234', '1677'];
array_values(array_intersect(array_map(function($item){ return explode(',', $item)[0]; }, $a), $b));
the output should be
=> [
"1234",
"1677",
]
I hope it's helpful
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
