0

I have two arrays:

$graph1 = Array ( 
[0] => Array ( [0] => 202101 [1] => 2 ) 
[1] => Array ( [0] => 202102 [1] => 3 ) 
[2] => Array ( [0] => 202103 [1] => 5 ) 
[3] => Array ( [0] => 202104 [1] => 2 ) 
[4] => Array ( [0] => 202105 [1] => 3 ) 
[5] => Array ( [0] => 202106 [1] => 4 ) 
[6] => Array ( [0] => 202107 [1] => 14 ) )

$graph2 = Array ( 
[0] => Array ( [0] => 202105 [1] => 3 ) 
[1] => Array ( [0] => 202107 [1] => 1 ) 
[2] => Array ( [0] => 202108 [1] => 6 ) 
)

and want to create a third array with an extra value where value[0] matches, and an extra row and extra value if there is no matching value[0]

I have tried array_merge and array_push but my understanding of arrays is not up to the task The output I want is like this:

$graph3 = Array ( 
[0] => Array ( [0] => 202101 [1] => 2  [2] => 0 ) 
[1] => Array ( [0] => 202102 [1] => 3  [2] => 0 ) 
[2] => Array ( [0] => 202103 [1] => 5  [2] => 0 ) 
[3] => Array ( [0] => 202104 [1] => 2  [2] => 0 ) 
[4] => Array ( [0] => 202105 [1] => 3  [2] => 3 ) 
[5] => Array ( [0] => 202106 [1] => 4  [2] => 0 ) 
[6] => Array ( [0] => 202107 [1] => 14 [2] => 1 )
[7] => Array ( [0] => 202108 [1] => 0  [2] => 6 )  )

Answers and explanation would be great - thank you

3
  • You need to use foreach loop along with array merge. Do you know array_intersect()? Commented Aug 18, 2021 at 9:17
  • Does this answer your question? PHP merge arrays by value and merge it's sub arrays Commented Aug 18, 2021 at 9:19
  • thank you @nice_dev - I did try this but couldn't work out how to have it add the 3rd value to the array - please see answer below which does that Commented Aug 18, 2021 at 10:51

1 Answer 1

1

This function should do the trick...

function combine_graphs( ...$arrays ) {
    $output = [];

    // First create keyed array
    foreach ( $arrays as $row ) {
        foreach ( $row as $columns ) {
            $key = $columns[ 0 ];
            if ( !array_key_exists( $key, $output ) ) {
                $output[ $key ] = [ $key ];
            }
        }
    }

    // Now add values
    foreach ( $output as $key => $values ) {
        foreach ( $arrays as $row ) {
            $index = array_search( $key, array_column( $row, 0 ) );
            $output[ $key ][] = $index !== false ? $row[ $index ][ 1 ] : 0;
        }
    }

    return array_values( $output );
}

Usage would be...

$graph1 = [
    [ 202101, 2 ],
    [ 202102, 3 ],
    [ 202103, 5 ],
    [ 202104, 2 ],
    [ 202105, 3 ],
    [ 202106, 4 ],
    [ 202107, 14 ],
];

$graph2 = [
    [ 202105, 3 ],
    [ 202107, 1 ],
    [ 202108, 6 ],
];

$graph3 = combine_graphs( $graph1, $graph2 );

The function allows any number of graphs to be combined for example...

$graph1 = [
    [ 202101, 2 ],
    [ 202102, 3 ],
    [ 202103, 5 ],
    [ 202104, 2 ],
    [ 202105, 3 ],
    [ 202106, 4 ],
    [ 202107, 14 ],
];

$graph2 = [
    [ 202105, 3 ],
    [ 202107, 1 ],
    [ 202108, 6 ],
];

$graph3 = [
    [ 202105, 3 ],
    [ 202107, 4 ],
    [ 202108, 9 ],
    [ 202107, 12 ],
    [ 202104, 2 ],
];

$graph4 = combine_graphs( $graph1, $graph2, $graph3 );

The above would output...

Array
(
    [0] => Array
        (
            [0] => 202101
            [1] => 2
            [2] => 0
            [3] => 0
        )

    [1] => Array
        (
            [0] => 202102
            [1] => 3
            [2] => 0
            [3] => 0
        )

    [2] => Array
        (
            [0] => 202103
            [1] => 5
            [2] => 0
            [3] => 0
        )

    [3] => Array
        (
            [0] => 202104
            [1] => 2
            [2] => 0
            [3] => 2
        )

    [4] => Array
        (
            [0] => 202105
            [1] => 3
            [2] => 3
            [3] => 3
        )

    [5] => Array
        (
            [0] => 202106
            [1] => 4
            [2] => 0
            [3] => 0
        )

    [6] => Array
        (
            [0] => 202107
            [1] => 14
            [2] => 1
            [3] => 4
        )

    [7] => Array
        (
            [0] => 202108
            [1] => 0
            [2] => 6
            [3] => 9
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Coleperfect - exactly what I needed - thank you
sorry for mis-typing @LeviCole - thank you - exactly and precisely the result needed and great that the function will combine many arrays - appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.