Skip to content
Advertisement

Line up two tables

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?

Advertisement

Answer

  • Get all years from both arrays, make them unique to remove the doubles and sort them numeric.

  • Iterate through all years. If not both have the same year, just continue, else keep them both and add to the results.

$result1 = [];
$result2 = [];

$years   = array_unique(array_merge(array_column($array1, 'date'), array_column($array2, 'date')));
sort($years, SORT_NUMERIC);

foreach ($years as $year) {
    $hasDate = function($date) use ($year) {
        return $date['date'] === $year;
    };

    $year1 = array_filter($array1, $hasDate);
    $year2 = array_filter($array2, $hasDate);

    if (empty($year1) || empty($year2)) {
        continue;
    }

    $result1[] = current($year1);
    $result2[] = current($year2);
}

print_r($result1);
print_r($result2);

The results look like

Array
(
    [0] => Array
        (
            [date] => 2023
            [income] => 123
        )

    [1] => Array
        (
            [date] => 2024
            [income] => 123
        )

    [2] => Array
        (
            [date] => 2026
            [income] => 123
        )

)
Array
(
    [0] => Array
        (
            [date] => 2023
            [income] => 321
        )

    [1] => Array
        (
            [date] => 2024
            [income] => 321
        )

    [2] => Array
        (
            [date] => 2026
            [income] => 321
        )

)
Advertisement