I have two following arrays:
$array1 = [
['date' => '2021', 'income' => 123],
['date' => '2022', 'income' => 123],
['date' => '2023', 'income' => 123],
['date' => '2024', 'income' => 123],
['date' => '2025', 'income' => 123],
['date' => '2026', 'income' => 123],
['date' => '2026', 'income' => 123],
];
$array2 = [
['date' => '2019', 'income' => 321],
['date' => '2020', 'income' => 321],
['date' => '2023', 'income' => 321],
['date' => '2024', 'income' => 321],
['date' => '2026', 'income' => 321],
['date' => '2027', 'income' => 321],
['date' => '2028', 'income' => 321],
];
Now I want to take the common part of them, based on the date value, starting from the top and bottom, so the result should look like this:
$result1 = [
['date' => '2023', 'income' => 123],
['date' => '2024', 'income' => 123],
];
$result2 = [
['date' => '2023', 'income' => 321],
['date' => '2024', 'income' => 321],
];
I have started with something like this:
while($array1[0]['date'] !== $array2[0]['date']) {
if (count($array1) > count($array2)) {
array_shift($array1);
} else {
array_shift($array2);
}
}
while ($array1[count($array1)-1]['date'] !== $array2[count($array2)-1]['date']) {
if (count($array1) > count($array2)) {
array_pop($array1);
} else {
array_pop($array2);
}
}
But it does not work, if there is a difference in the middle, how can I repair this?