结合 PHP 中匹配键值的两个多维数组

Combine Two Multidimensional Arrays on matching key values in PHP

问题

我有两个像这样的多维数组,

第一个数组

array(
      [0] => Array
        (
        [course_id] => 10
        [topic] => Booda-hood, advice and an important pit.
        [lesson_id] => 10
    )

[1] => Array
    (
        [course_id] => 10
        [topic] => new topic
        [lesson_id] => 11
    )

)

第二个数组

  Array
    (
    [0] => Array
      (
        [id] => 10
        [description] => Lorem Ipsum is blablabla
        [course_title] => Lorem Ipsum is 
    )

[1] => Array
    (
        [id] => 11
        [description] => Lorem Ipsum is simply dummy text of 
        [course_title] => Lorem Ipsum 
    )

)

我想像这样组合两个数组 if($1st[0]['lesson_id'] == $2nd[0]['id'])

第三数组

      array(
          [0] => Array
          (
           [course_id] => 10
           [topic] => Booda-hood, advice and an important pit.
           [lesson_id] => 10
           [id] => 10
           [description] => Lorem Ipsum is blablabla
           [course_title] => Lorem Ipsum is 
       )

[1] => Array
    (
        [course_id] => 10
        [topic] => new topic
        [lesson_id] => 11
        [id] => 11
        [description] => Lorem Ipsum is simply dummy text of 
        [course_title] => Lorem Ipsum
    )

)

我希望我能解释一切!

此解决方案适合您--

$a = [
            [
                'course_id' => 10,
                'topic' => 'Booda-hood, advice and an important pit',
                'lesson_id' => 10,
            ],
            [
                'course_id' => 10,
                'topic' => 'new topic',
                'lesson_id' => 11,
            ]
        ];

        $b = [
            [
                'id' => 10,
                'description' => 'Lorem Ipsum is blablabla',
                'course_title' => 'Lorem Ipsum is',
            ],
            [
                'id' => 11,
                'description' => 'Lorem Ipsum is simply dummy text of',
                'course_title' => 'Lorem Ipsum',
            ]
        ];

        $a3 = [];
        foreach ($a as $key => $a1) {
            $search = $this->searchValueInArray($a1['lesson_id'], $b);
            if ($search != null) {
                $a3[] = array_merge($a1, $search);
            } else {
                $a3[] = $a1;
            }
        }
        print_r($a3);

function searchValueInArray($value, $array) {
        foreach ($array as $arr) {
            if ($arr['id'] == $value) {
                return $arr;
            }
        }
        return null;
    }

希望对你有用。

<?php
// Your array
$arr1 = [
  [
  'course_id' => 10,
  'topic' => 'Booda-hood, advice and an important pit.',
  'lesson_id' => 10
  ],
  [
  'course_id' => 10,
  'topic' => 'Booda-hood, advice and an important pit.',
  'lesson_id' => 11
  ]
];

$arr2 = [
  [
  'id' => 10,
  'description' => '10 Lorem Ipsum is blablabla',
  'course_title' => '10 Lorem Ipsum is blablabla'
  ],
  [
  'id' => 11,
  'description' => '11 Lorem Ipsum is blablabla',
  'course_title' => '11 Lorem Ipsum is blablabla'
  ]
];
 
// Combining arrays implementation 

foreach ( $arr2 as $k=>$v )
{
  // rename array2 `id` to `lesson_id`
  $arr2[$k]['lesson_id'] = $arr2[$k] ['id'];
  unset($arr2[$k]['id']);
}

// array_replace_recursive — Replaces elements from passed arrays into the first array recursively   
print_r(array_replace_recursive($arr1, $arr2));
?>

输出

[
  {
    "course_id": 10,
    "topic": "Booda-hood, advice and an important pit.",
    "lesson_id": 10,
    "description": "10 Lorem Ipsum is blablabla",
    "course_title": "10 Lorem Ipsum is blablabla"
  },
  {
    "course_id": 10,
    "topic": "Booda-hood, advice and an important pit.",
    "lesson_id": 11,
    "description": "11 Lorem Ipsum is blablabla",
    "course_title": "11 Lorem Ipsum is blablabla"
  }
]

希望对您有所帮助!

参考 - array_replace_recursive