Skip to content

Instantly share code, notes, and snippets.

@asbp
Created July 1, 2020 04:32
Show Gist options
  • Save asbp/e77020ec3598db937fd0222cc3f29d56 to your computer and use it in GitHub Desktop.
Save asbp/e77020ec3598db937fd0222cc3f29d56 to your computer and use it in GitHub Desktop.
<?php
$array = array(
array(
'kode' => 'FIS15101',
'nama' => 'Fisika Dasar',
'sks' => '2',
'nilai' => '4',
'ak' => '8',
),
array(
'kode' => 'FIS15101L',
'nama' => 'Praktikum Fisika Dasar',
'sks' => '1',
'nilai' => '3',
'ak' => '3',
),
array(
'kode' => 'TW15101',
'nama' => 'Praktek Tilawah',
'sks' => '0',
'nilai' => '',
'ak' => '',
),
array(
'kode' => 'FIS15101L',
'nama' => 'Praktikum Fisika Dasar',
'sks' => '1',
'nilai' => '4',
'ak' => '4',
),
);
echo "<pre>";
print_r($array);
echo "<br>";
print_r(unique($array));
//based from https://stackoverflow.com/questions/55218253/find-duplicates-in-associative-array-php-compare-their-values
function unique($matkulArray) {
$keep = [];
$tmp = array_column($matkulArray, 'kode');
$dupes = array_diff(array_count_values($tmp), [1]);
foreach($dupes as $key => $val){
$res[$key] = max(array_intersect_key(array_column($matkulArray, 'nilai'), array_intersect($tmp, [$key])));
$keep = array_filter(
$matkulArray,
function ($item) use ($res, $key) {
return $item['kode'] == $key && $item['nilai'] == $res[$key];
}
);
}
foreach($matkulArray as $key => $val) {
if(!array_key_exists($val['kode'], $res) || array_key_exists($key, $keep)) continue;
unset($matkulArray[$key]);
}
return array_values($matkulArray);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment