0

I need to replace my main array elements by 'employee_id'. I already tried array_replace_recursive() function But It replaces arrays by the default index value.

$merged = array_replace_recursive($empList, $getworkedHrs, $getErrAttnds);

The arrays look like this: $empList

    array (
      0 => 
      array (
        'employee_id' => '254',
        'emp_name' => 'Mary McDonald',
        'worked_days' => 0,
        'worked_hours' => 0,
        'error_attendance' => 0,
      ),
      1 => 
      array (
        'employee_id' => '255',
        'emp_name' => 'Anthony Coffman',
        'worked_days' => 0,
        'worked_hours' => 0,
        'error_attendance' => 0,
      ),
      2 => 
      array (
        'employee_id' => '316',
        'emp_name' => 'cheth aruno',
        'worked_days' => 0,
        'worked_hours' => 0,
        'error_attendance' => 0,
      ),
   )

2nd Array: $getworkedHrs

array (
  0 => 
  array (
    'employee_id' => '254',
    'worked_days' => '22',
    'worked_hours' => '7.0',
  ),
  1 => 
  array (
    'employee_id' => '255',
    'worked_days' => '8',
    'worked_hours' => '7.0',
  ),
)

3rd array : $getErrAttnds

array (
  0 => 
  array (
    'employee_id' => '316',
    'error_attendance' => '1',
  ),
)

Expected Result Array:

    array (
      0 => 
      array (
        'employee_id' => '254',
        'emp_name' => 'Mary McDonald',
        'worked_days' => 22,
        'worked_hours' => 7.0,
        'error_attendance' => 0,
      ),
      1 => 
      array (
        'employee_id' => '255',
        'emp_name' => 'Anthony Coffman',
        'worked_days' => 8,
        'worked_hours' => 7.0,
        'error_attendance' => 0,
      ),
      2 => 
      array (
        'employee_id' => '316',
        'emp_name' => 'cheth aruno',
        'worked_days' => 0,
        'worked_hours' => 0,
        'error_attendance' => 1,
      ),
   )
2
  • 1
    Print_r is good if we just want to look at the arrays, use var_export and update all your arrays. That way we don't have to retype everything by hand Commented Jun 2, 2020 at 17:31
  • Sure. I changed. Thank you Commented Jun 2, 2020 at 18:23

1 Answer 1

1

I believe the simplest solution is to merge them in two foreach.

First make the main array associative so that it's easier to get the correct employee.

Then loop the arrays and copy the values.

$empList = array_column($empList, null, 'employee_id');

foreach($getworkedHrs as $entry){
    $empList[$entry['employee_id']]['worked_days'] = $entry['worked_days'];
    $empList[$entry['employee_id']]['worked_hours'] = $entry['worked_hours'];
}

foreach($getErrAttnds as $entry){
    $empList[$entry['employee_id']]['error_attendance'] = $entry['error_attendance'];
}

var_export($empList);

https://3v4l.org/gIXP6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.