-1

I have two array students and marks . Need a output array like marks_sheet.Delete all data which is not present in marks array and insert a new field marks. // name of this array students

Array    
    (
        [0] => Array
            (
                [userId] => user1
                [name] => Suman Mandal
                [age] => 20    

            )
        [1] => Array
            (

                [userId] => user2
                [name] => Amit Halder
                [age] => 30

            )
        [2] => Array
            (

                [userId] => user3
                [name] => Asif Rahman
                [age] => 25

            )
        [3] => Array
            (

                [userId] => user4
                [name] => Gopal Ghosh
                [age] => 21

            )
    )

// name of this array marks

 Array   
    (
        [0] => Array
            (
                [userId] => user1 
                [marks]=>80 

            )
        [1] => Array
            (

                [userId] => user3  
                [marks]=>90          
            )

    )

I need a output like this: // name of this array marks_sheet

 Array   
    (
        [0] => Array
            (
                [userId] => user1 
                [name] => Suman Mandal
                [age] => 20
                [marks]=>80 

            )
        [1] => Array
            (

                [userId] => user3  
                [name] => Asif Rahman
                [age] => 25
                [marks]=>90          
            )

    )

I have a little knowledge on php. May be this is so easy but I am feeling hard to solve it... please help me

Suman Mandal
  • 25
  • 1
  • 5

1 Answers1

0

You can use array_replace_recusrive on the userId indexed arrays.

First use array_column to index the arrays by the userId.

Then use array_replace_recursive to merge the values in the arrays and retain the key indexes of both array sets.

Lastly use array_intersect_key to only return the array values from the $marks array.

Example https://3v4l.org/adrYV

$students = array_column($students, null, 'userId');
$marks = array_column($marks, null, 'userId');
$marks_sheet = array_intersect_key(array_replace_recursive($students, $marks), $marks);

Result

Array
(
    [user1] => Array
        (
            [userId] => user1
            [name] => Suman Mandal
            [age] => 20
            [marks] => 80
        )

    [user3] => Array
        (
            [userId] => user3
            [name] => Asif Rahman
            [age] => 25
            [marks] => 90
        )

)

If you need the end result to not be indexed by userId you can use array_values, which will reindex the array.

$marks_sheet = array_values(
    array_intersect_key(array_replace_recursive($students, $marks), $marks)
);

Result

Array
(
    [0] => Array
        (
            [userId] => user1
            [name] => Suman Mandal
            [age] => 20
            [marks] => 80
        )

    [1] => Array
        (
            [userId] => user3
            [name] => Asif Rahman
            [age] => 25
            [marks] => 90
        )

)

Alternatively you can iterate over the marks and students to build your marks_sheet array.

Example https://3v4l.org/Xmg3a

$marks_sheet = [];
foreach ($marks as $mark) {
    foreach ($students as $student) {
        if ($student['userId'] === $mark['userId']) {
            $mark_sheet = $student;
            foreach ($mark as $key => $value) {
                $mark_sheet[$key] = $value;
            }
            $marks_sheet[] = $mark_sheet;
        }
    }
}

Result

Array
(
    [0] => Array
        (
            [userId] => user1
            [name] => Suman Mandal
            [age] => 20
            [marks] => 80
        )

    [1] => Array
        (
            [userId] => user3
            [name] => Asif Rahman
            [age] => 25
            [marks] => 90
        )

)
Will B.
  • 17,883
  • 4
  • 67
  • 69