John Davidson

php find id in 2 arrays that don't match

0 comments
Message:


I need to find an ID of an array that does not exist in the second array then know the ID. So for example eventid would be the same as id, I need to know if id does not exist in the second array but it does exist in the first array.


so 10001 = 10001, 10002 = 10002, 10003 = 10003, 10004 would be in the first array but not the second. How do I get the result of 1004 not being in the second array.


foreach($json as $eventinsert){   

$eventid = $eventinsert['objectId'];

$event = new Google_Service_Calendar_Event(array(
'Id' => $eventinsert['objectId'],
'summary' => $eventinsert['name'],
'description' => $eventinsert['name'],
'start' => array(
'dateTime' => $eventinsert['dateStart']
),`enter code here`
'end' => array(
'dateTime' => $eventinsert['dateEnd']
)
));
foreach ($events as $event) {
$start = $event->start->dateTime;
$id = $event->id;
if (empty($start)) {
$start = $event->start->date;

}
// printf("%s (%s)\n", $event->getSummary(), $start);
// printf("%s (%s)\n", $event->getSummary(), $id);
}


My Answer:
You can achieve this by using the array_diff() function in PHP. Here's an example of how you can find the IDs that exist in the first array but not in the second array:

php
$firstArray = array(10001, 10002, 10003, 10004);
$secondArray = array(10001, 10002, 10003);

$missingIds = array_diff($firstArray, $secondArray);

foreach($missingIds as $id){
echo $id . " is in the first array but not in the second array.";
}


In this example, the $missingIds array will contain the IDs that exist in the $firstArray but not in the $secondArray. You can then loop through the $missingIds array to get the IDs that are missing in the second array.

Rate this post

5 of 5 based on 5659 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us