Home > OS >  PHP Combine Two Arrays Into One With Common ID as Text
PHP Combine Two Arrays Into One With Common ID as Text

Time:01-09

I have two arrays that I want to combine:

Here is the input of my two arrays (notice Factor1 and Factor2 are different, but YearMonth is the same in both arrays):

Array
(
    [0] => stdClass Object
        (
            [YearMonth] => 2022-01
            [Factor1] => 627
        )

    [1] => stdClass Object
        (
            [YearMonth] => 2021-12
            [Factor1] => 3006
        )

    [2] => stdClass Object
        (
            [YearMonth] => 2021-11
            [Factor1] => 2456
        )

    [3] => stdClass Object
        (
            [YearMonth] => 2021-10
            [Factor1] => 1482
        )

    [4] => stdClass Object
        (
            [YearMonth] => 2021-09
            [Factor1] => 1266
        )

    [5] => stdClass Object
        (
            [YearMonth] => 2021-08
            [Factor1] => 1981
        )
)

Array
(
    [0] => stdClass Object
        (
            [YearMonth] => 2022-01
            [Factor2] => 380
        )

    [1] => stdClass Object
        (
            [YearMonth] => 2021-12
            [Factor2] => 1773
        )

    [2] => stdClass Object
        (
            [YearMonth] => 2021-11
            [Factor2] => 769
        )

    [3] => stdClass Object
        (
            [YearMonth] => 2021-10
            [Factor2] => 700
        )

    [4] => stdClass Object
        (
            [YearMonth] => 2021-09
            [Factor2] => 608
        )

    [5] => stdClass Object
        (
            [YearMonth] => 2021-08
            [Factor2] => 859
        )
)

Here is what I would like the output to be:

Array
(
    [0] => stdClass Object
        (
            [YearMonth] => 2022-01
            [Factor1] => 627
            [Factor2] => 380
        )

    [1] => stdClass Object
        (
            [YearMonth] => 2021-12
            [Factor1] => 3006
            [Factor2] => 1773
        )

    [2] => stdClass Object
        (
            [YearMonth] => 2021-11
            [Factor1] => 2456
            [Factor2] => 769
        )

    [3] => stdClass Object
        (
            [YearMonth] => 2021-10
            [Factor1] => 1482
            [Factor2] => 700
        )

    [4] => stdClass Object
        (
            [YearMonth] => 2021-09
            [Factor1] => 1266
            [Factor2] => 608
        )

    [5] => stdClass Object
        (
            [YearMonth] => 2021-08
            [Factor1] => 1981
            [Factor2] => 859
        )
)

I've tried array_merge() but it just appends the second array to the end of the first.

And when I try:

$array3 = [];
foreach (array_merge($array1, $array2) as $row) {
    $array3[$row['YearMonth']] = ($array3[$row['YearMonth']] ?? [])   $row;
}

I receive this error:

<b>Fatal error</b>:  Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/wp-content/plugins/ad-inserter-pro/class.php(606) : eval()'d code:23

Any help you can provide is appreciated. Thank you.

CodePudding user response:

Try something like this

// loop thru 1st array using & to treat value as a reference, so that we can modify values in that array 
foreach ($array1 as &$value) {

  // loop through 2nd array
  foreach ($array2 as $value2) {

    // look for matching YearMonth (use -> because they are objects)
    if ($value->YearMonth === $value2->YearMonth) {

       // when a match is found, copy the Factor2 value
       $value->Factor2 = $value2->Factor2

       // for efficiency exit the 2nd loop, because there is no point checking any more rows if we have already found the match
       break;
    }
  }

  // after exiting the 2nd loop, code will carry on here, and keep checking values in the 1st loop

}
  •  Tags:  
  • Related