Search code examples
phparraysmultidimensional-arrayarray-merge

Append row data from one 2d array to another 2d array where a corresponding column value is found


I have two arrays:

  1. [A] -from the mysql query of one database on server1- the $postings_array - a SELECT of all postings of a discussion-thread (based on the thread's id)
  2. [B] -from the mysql query of an other database on server2 - the $usersdata_array - a SELECT of all postings of a discussion-thread (based on the thread's id)

This means:

  • in [A] there are many postings-sub-arrays and in [B] one or more userdata-sub-arrays.
  • both arrays always do include a key named usrsID in each of their subarrays.
I need to extend the Sub-Arrays in $postings_array [A]

by merging them 

with the Sub-Arrays of the $usersdata_array [B]

based on WHERE the VALUE of the usrsID KEY in the sub-array[A] is EQUAL to the usrsID KEY in the sub-array[B].

EXAMPLE:

Array [A]:
(
    [0] => Array
        (
            [ID] => 5
            [usrsID] => 3
            [tid] => 19
            [txtid] => 22
        )
    [1] => Array
        (
            [ID] => 6
            [usrsID] => 1
            [tid] => 19
            [txtid] => 23
        )
    [2] => Array
        (
            [ID] => 7
            [usrsID] => 2
            [tid] => 19
            [txtid] => 24
        )
    [3] => Array
        (
            [ID] => 8
            [usrsID] => 1
            [tid] => 19
            [txtid] => 25
        )
)

--

Array [B]:
(
    [0] => Array
        (
            [id] => 1
            [usrsID] => 1
            [avatarID] => 1

        )
    [1] => Array
        (
            [id] => 2
            [usrsID] => 2
            [avatarID] => 3

        )
    [2] => Array
        (
            [id] => 3
            [usrsID] => 3
            [avatarID] => 22

        )

)

needed result (the by [B] extended [A] for the example above):

Array [A_extended]:
(
    [0] => Array
        (
            [ID] => 5
            [usrsID] => 3
            [tid] => 19
            [txtid] => 22
            [id] => 3
            [avatarID] => 22
        )
    [1] => Array
        (
            [ID] => 6
            [usrsID] => 1
            [tid] => 19
            [txtid] => 23
            [id] => 1
            [avatarID] => 1
        )
    [2] => Array
        (
            [ID] => 7
            [usrsID] => 2
            [tid] => 19
            [txtid] => 24
            [id] => 2
            [avatarID] => 3
        )
    [3] => Array
        (
            [ID] => 8
            [usrsID] => 1
            [tid] => 19
            [txtid] => 25
            [id] => 1
            [avatarID] => 1
        )
)

... I think, it's a common problem so there should be a best-practice around (may be in one in-build PHP function or a combination of two or three of them) - and I do not have to reinvent the wheel.

else, my approach would be

  1. check the amounts of iterations (= the subarrays found in the $usersdata_array [B] )
  2. iterate over the outerHaystack and trigger a function when $needle was found in innerHaystack
  3. perform merge via checkSubArrayfunc

Approach, with hayStackArray = complete [A]Array; needle = $usrsID value of [B] Sub-Array:

function checkSubArrayfunc($hayStackSubArray, $needle, $toMergeSubArray) {

        if (in_array(array('$hayStackSubArray'), $needle)) {
                array_merge_recursive($hayStackSubArray, $toMergeSubArray);
        }
    }


Solution

  • Try this:

    foreach($arr_b as $b_item) {
        foreach($arr_a as $key => &$a_item) {
            if ($b_item['usrsID'] == $a_item['usrsID']) {
                $a_item['id'] = $b_item['usrsID'];
                $a_item['avatarID'] = $b_item['avatarID'];
            }
        }
    }
    

    Your output of $_arr_a will be:

    Array
    (
        [0] => Array
            (
                [ID] => 5
                [usrsID] => 3
                [tid] => 19
                [txtid] => 22
                [id] => 3
                [avatarID] => 22
            )
    
        [1] => Array
            (
                [ID] => 6
                [usrsID] => 1
                [tid] => 19
                [txtid] => 23
                [id] => 1
                [avatarID] => 1
            )
    
        [2] => Array
            (
                [ID] => 7
                [usrsID] => 2
                [tid] => 19
                [txtid] => 24
                [id] => 2
                [avatarID] => 3
            )
    
        [3] => Array
            (
                [ID] => 8
                [usrsID] => 1
                [tid] => 19
                [txtid] => 25
                [id] => 1
                [avatarID] => 1
            )
    
    )