0

I need to merge the data between two 2d array related by a shared column value. If there is no shared value found, the result array should apply a default value of zero for the new column.

$mahasiswa = [
    ['npm' => 1123123123, 'nama' => 'LINDA'],
    ['npm' => 121323131, 'nama' => 'SETIADI'],
    ['npm' => 12312312, 'nama' => 'WIWIN'],
    ['npm' => 12345678910, 'nama' => 'ABDUL IMAN'],
    ['npm' => 12355141, 'nama' => 'KOKOM'],
    ['npm' => '123U4148U90', 'nama' => 'JIHAN'],
    ['npm' => 978687999, 'nama' => 'KURNIA']
];

$piutang = [
    ['total' => 14600000, 'nama' => 'KOKOM', 'npm' => 12355141],
    ['total' => 8000000, 'nama' => 'KURNIA', 'npm' => 978687999]
];

My current code:

$i = 0;
$rekappiutang = array();
foreach ($mahasiswa as $row) {
   foreach ($piutang as $key=>$val) {
      if ($val['npm'] == $row['npm']) {
         $rekappiutang[$i]['npm']  = $row['npm'];
         $rekappiutang[$i]['nama'] = $row['nama'];
         $rekappiutang[$i]['totalpiutang']  = $val['total'];
       } else {
         $rekappiutang[$i]['npm']  = $row['npm'];
         $rekappiutang[$i]['nama'] = $row['nama'];
         $rekappiutang[$i]['totalpiutang'] = 0;
       }                  
   }
   $i++;
}

Unfortunately, no all related totals re correctly applied to the new column.

My desired result:

[
    ['npm' => 1123123123, 'nama' => 'LINDA', 'total' => 0],
    ['npm' => 121323131, 'nama' => 'SETIADI', 'total' => 0],
    ['npm' => 12312312, 'nama' => 'WIWIN', 'total' => 0],
    ['npm' => 12345678910, 'nama' => 'ABDUL IMAN', 'total' => 0],
    ['npm' => 12355141, 'nama' => 'KOKOM', 'total' => 0],
    ['npm' => '123U4148U90', 'nama' => 'JIHAN', 'total' => 0],
    ['npm' => 978687999, 'nama' => 'KURNIA', 'total' => 0]
]
2
  • Is $v['npm'] == $row['npm'] a typo in your question? it should be $val['npm']. Commented Dec 1, 2013 at 17:58
  • oh yes....it's typo in my question..not in my code... Commented Dec 1, 2013 at 18:01

2 Answers 2

2

The problem is your index, each time you get inside the second foreach you overwrite the index position, for example if you get in the first iteration your index will be 0, in the second iteration your index will still be 0 and in this case you'll loose the information you collected in the first iteration, you can do two things depending on how you want to condition your array, you could break outside the first foreach if you find something (if that's what you want to obtain) like this:

if ($val['npm'] == $row['npm']) {
     $rekappiutang[$i]['npm']  = $row['npm'];
     $rekappiutang[$i]['nama'] = $row['nama'];
     $rekappiutang[$i]['totalpiutang']  = $val['total'];
    break;
}

Or you could increment the index at the end of the first foreach thus having all the result stored and no overwriting.

KURNIA works only because it's the last element of the array and after that the foreach breaks automatically.

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

Comments

0

Making value-based searches will be one of the least efficient ways to map one array's values to another array.

Instead, create a lookup map with nama values as keys and total values as values. Then iterate the main array and append to each row either the found value or the default 0 value. Demo

$map = array_column($piutang, 'nama', 'total');

var_export(
    array_map(
        fn($row) => $row + ['total' => $map[$row['nama']] ?? 0],
        $mahasiswa
    )
);

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.