I have a multi dimensional array. I need to search through the arrays and retrieve instance of dates that match each other.
This is my array:
$dateArray = array(8) {
[0]=>
array(15) {
["bookable_id"]=>
string(1) "1"
["event_date"]=>
string(10) "2019-09-11"
}
[1]=>
array(15) {
["bookable_id"]=>
string(1) "1"
["event_date"]=>
string(10) "2019-09-11"
}
[2]=>
array(15) {
["bookable_id"]=>
string(1) "4"
["event_date"]=>
string(10) "2019-10-17"
}
[3]=>
array(15) {
["bookable_id"]=>
string(1) "4"
["event_date"]=>
string(10) "2019-10-17"
}
[4]=>
array(15) {
["bookable_id"]=>
string(1) "3"
["event_date"]=>
string(10) "2020-09-15"
}
[5]=>
array(15) {
["bookable_id"]=>
string(1) "3"
["event_date"]=>
string(10) "2020-09-15"
}
[6]=>
array(15) {
["bookable_id"]=>
string(1) "2"
["event_date"]=>
string(10) "2021-09-09"
}
}
This is my function:
function searchMatchingDates($compare, $array)
{
foreach ($array as $val) {
if ($compare["event_date"]== $val["event_date"]){
return $val;
}
}
return null;
And this is how i searched it:
foreach ($dateArray as $compare) {
$match = searchMatchingDates($compare, $array);
}
if($match){
echo "$compare['bookable_id'] + $match['bookable_id']; "
}
This works and return the date. It however return duplicate dates as the array will compare twice.
I need a way to remove an array once an array has been compared once .
For example, once array once has ran its iteration it needs to be removed so that the next array does not do a comparison on it.