【问题标题】:Combine Two Multidimensional Arrays on matching key values in PHP [closed]在 PHP 中结合两个多维数组匹配键值 [关闭]
【发布时间】:2021-08-05 05:17:22
【问题描述】:

问题

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

第一个阵列

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
    )

)

我希望我能解释一切!

【问题讨论】:

  • 到目前为止你尝试过什么?请出示代码。
  • 创建一个你尝试过的minimal reproducible example
  • @unclexo 我想加入匹配键的数组,基本上,第一个数组是学生的进度,第二个是课程,所以我想检查学生的基础知识进度向学生展示的课程。如果学生在课程中有任何进步,则表明其他任何事情都不会发生。

标签: php arrays multidimensional-array array-combine


【解决方案1】:
<?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

【讨论】:

  • 你能回答这个link吗?
  • 感谢您的回答,它按我的意愿工作。
【解决方案2】:

这个解决方案对你有用--

$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;
    }

希望它对你有用。

【讨论】:

  • 这给了我致命错误 foreach() 参数必须是数组|对象类型,给定字符串用于您使用的 searchValueInArray 方法
  • 请检查$b(第二个数组)必须是一个数组
  • 错误是$this->searchValueInArray($a1['lesson_id'], $b);应该是这样的$this->searchValueInArray( $b,$a1['lesson_id']);
  • 当其中一个数组为空时,您的答案也会给出一个空数组
  • 是的,您可能需要添加检查,我没有在需要的地方添加检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
相关资源
最近更新 更多