-2

Problem

i have two multidimensional arrays that look like this,

1st Array

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
    )

)

2nd array

  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 
    )

)

I want to combine both arrays like this if($1st[0]['lesson_id'] == $2nd[0]['id'])

3rd array

      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
    )

)

I hope I explain everything!

Atif Raja
  • 17
  • 4
  • What have you tried so far? Show the code please. – unclexo Aug 05 '21 at 05:22
  • Create a [mcve] of what you tried – Alon Eitan Aug 05 '21 at 05:31
  • @unclexo I want to join the array on matching keys, basically, the first array is the progress of students and the second one is the course lessons so I want to check the progress of students on basics of the lesson to show the student. if student have any progress in lesson then show else nothing will happen. – Atif Raja Aug 05 '21 at 05:37

2 Answers2

0

This solution will work for you--

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

Hope it will work for you.

PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • that's giving me Fatal error **foreach() argument must be of type array|object, string given** for the **searchValueInArray** method you used – Atif Raja Aug 05 '21 at 05:49
  • please check the $b (second array) must be an array – PHP Geek Aug 05 '21 at 05:53
  • the mistake was **$this->searchValueInArray($a1['lesson_id'], $b);** it should be like this **$this->searchValueInArray($b,$a1['lesson_id']);** – Atif Raja Aug 05 '21 at 06:08
  • your answer also gives an empty array when one of the arrays is empty – Atif Raja Aug 05 '21 at 06:09
  • yes, you might need to add checks, I did not add checks wherever required – PHP Geek Aug 05 '21 at 06:14
  • and if second array is empty then that would not returned empty – PHP Geek Aug 05 '21 at 06:14
  • Yeah, you are right. Thanks for your time, Can you Answer this [get-data-from-multiple-tables](https://stackoverflow.com/questions/68653597/get-data-from-multiple-tables-in-mysql-with-different-where-conditions?noredirect=1#comment121330679_68653597)? that is a part of this question. – Atif Raja Aug 05 '21 at 06:18
0
<?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));
?>

OUTPUT

[
  {
    "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"
  }
]

Hope this helps!

Ref - array_replace_recursive

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47