php 组合两个数组的子数组,其中某个键的值相等

php combine two array's subarrays where value of certain key is equal

您好,感谢您的关注

我有两个数组:

  1. [A] - 来自 server1 上一个数据库的 mysql 查询 - $postings_array - 讨论线程的所有帖子的 SELECT (基于线程的 id)
  2. [B] - 来自 server2 上其他数据库的 mysql 查询 - $usersdata_array - 讨论线程的所有帖子的 SELECT (基于线程的 id)

这意味着:

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].

示例:

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

        )

)

需要的结果(上面例子中的 [B] 扩展 [A]):

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

...我认为,这是一个常见问题,因此应该有一个最佳实践(可能在一个内置 php 函数中或两个或三个函数的组合中)——我确实这样做不必重新发明轮子。 至少,我希望如此...

否则,我的做法是

  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

方法, with hayStackArray = complete [A]Array; needle = [B] 子数组的 $usrsID 值:

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

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

首先取一个空数组作为store marged array。然后在两个数组中遍历。如果 userID 在两个元素数组中都相同,则合并这些数组并推入 margeArr.

$margeArr = [];
foreach($Array_A as $a){
  foreach($Array_B as $b){
    if($a['usrsID'] == $b['usrsID']){
      array_push($margeArr,array_merge($a,$b));
    }
  }
}
print_r($margeArr);

Comment by the questionaire: It's a great solution - it really meets the requirements described in my question and merges the to be marged Array with the key-value-pairs of the "to be injected"-Array. But I prefere the bestprogrammersinintheworld's solution, where I can rename the keys "en passant / on-the-fly":

foreach($arr_b as $b_item) {
    foreach($arr_a as $key => &$a_item) {
        if ($b_item['usrsID'] == $a_item['usrsID']) {
            $a_item['myNewKeyName1'] = $b_item['usrsID'];
            $a_item['myNewKeyName2'] = $b_item['avatarID'];
        }
    }
}
print("<pre>".print_r($arr_a,true)."</pre>"); 

试试这个:

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'];
        }
    }
}

$_arr_a 的输出将是:

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
        )

)