Ok. I have 2 arrays. One would be the original data, the other would be the new data. Between the 2 of them there could be matching values. And what I need to do, is figure out from the new inbound array whats new, whats a match, and whats not a match. right now I am partially fried from coding all day so I am kinda at that burnt out state where logic makes sense but doesn't so bare with me please.
The end goal is that I need these 3 arrays, one of items removed from the original, one of items that are new entries, and then the original entries that matched anything in the new entries.
Im was thinking something to the extent of starting off with 3 blank arrays to fill as I cycle over the original or new with a loop and compare them adding each type old, new, removing to there respective array so I can work with the results there after. Which I can piece together an idea in my head that works for 2 arrays as the output, but the 3rd I get lost on. So Im kinda hindged on this bit for the moment. looking for advice and or help Creating a loop or something better that will yield the 3 sets of results I am looking for.
Edit adding in concept arrays
Array of original data from database:
Array(
15,
22,
100,
1500,
2000,
500,
3000,
1101
)
Array of inbound new data
Array(
100,
800,
920,
1500,
2000,
1603,
500,
3000,
1101
)
Array of expected matches between original and inbound new:
Array(
100,
1500,
2000,
500,
3000,
1101
)
Array of expected "new" entries between original and new:
Array(
100,
800,
920,
1603
)
Array of expected entries from old that don't match between original and new:
Array(
15,
22
)
It may not be spot on as I obviously just hand typed these.. but hopefully it demonstrates what Im trying to achieve to some extent
Barmar's comment is correct, array_diff
and array_intersect
are what you want. You have a typo in your question though, "100" appears in both the matches and the new values arrays.
<?php
$orig = array(15, 22, 100, 1500, 2000, 500, 3000, 1101);
$post = array(100, 800, 920, 1500, 2000, 1603, 500, 3000, 1101);
// Gets things in both arrays
$matches = array_intersect($orig, $post);
// Gets things in $post that aren't in $orig
$new = array_diff($post, $orig);
// Gets things in $orig that aren't in $post
$removed = array_diff($orig, $post);
foreach (array($orig, $post, $matches, $new, $removed) as $array) {
print_r($array);
}
My guess... you're processing checkboxes or multi-selects to determine what to keep, what to add and what to remove.