Home > Software engineering >  Line up two tables
Line up two tables

Time:08-20

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?

CodePudding user response:

  • 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
        )

)

CodePudding user response:

Use nested loops to find the first matching elements in the two arrays.

Then iterate from there as long as the years continue to match, and push these onto the two result arrays.

<?php

$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],
];

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

foreach ($array1 as $i => $item1) {
    $found = false;
    foreach ($array2 as $j => $item2) {
        if ($item1['date'] == $item2['date']) {
            $found = true;
            break;
        }
    }
    if ($found) {
        echo "Found at $i and $j\n";
        for (; $i < count($array1) && $j < count($array2) && $array1[$i]['date'] == $array2[$j]['date']; $i  , $j  ) {
            $result1[] = $array1[$i];
            $result2[] = $array2[$j];
        }
        break;
    }
}

print_r($result1);
print_r($result2);
  • Related