php - How to get rid of keys and values ??�while leaving some keys and values ??�in a multidimensional array
So I have a multidimensional array like this:
Array
(
[0] => Array
(
[id_user] => 2112100001
[nik] => 3216060611900007
[nama] => Jauharuddien Sofari
[tempat_lahir] => Klaten
[tanggal_lahir] => 1995-12-21
[jk] => L
[id_agama] => 1
[no_telp] => 081123123123
[alamat] => elapa dua wetan jakarta
[tanggal_masuk] => 2020-11-03
[gaji_pokok] => 4500000.00
[id_role] => SPV
[score] => 0.5
[created_at] => 2021-12-10 09:46:25
[created_by] => 2112010001
[update_at] => 2022-01-07 21:00:19
[update_by] => 2201050001
[id_department] => 1
[nama_department] => Quality Control
[id_jabatan] => 1
[nama_jabatan] => Supervisor
[status] => 4
[total_overtime] => 6
[val_overtime] => 4
[val_workexp] => 2
[val_age] => 2
[val_salary] => 2
)
[1] => Array
(
[id_user] => 2112100008
[nik] => 3216060611900014
[nama] => Biantono
[tempat_lahir] => Jakarta
[tanggal_lahir] => 1982-12-08
[jk] => L
[id_agama] => 1
[no_telp] => 081123123123
[alamat] => Pasar rebo
[tanggal_masuk] => 2020-02-12
[gaji_pokok] => 5000000.00
[id_role] => SPV
[score] => 0.29
[created_at] => 2021-12-10 10:07:35
[created_by] => 2112010001
[update_at] => 2022-01-07 21:00:19
[update_by] => 2201050001
[id_department] => 4
[nama_department] => Human Resource
[id_jabatan] => 1
[nama_jabatan] => Supervisor
[status] => 4
[total_overtime] => 6
[val_overtime] => 4
[val_workexp] => 2
[val_age] => 4
[val_salary] => 3
)
[2] => Array
(
[id_user] => 2112100005
[nik] => 3216060611900011
[nama] => Agung Nugroho
[tempat_lahir] => Yogyakarta
[tanggal_lahir] => 1985-07-31
[jk] => L
[id_agama] => 1
[no_telp] => 081123123123
[alamat] => Jl. raya jababeka 2 no.112
[tanggal_masuk] => 2008-09-10
[gaji_pokok] => 6000000.00
[id_role] => SPV
[score] => 0.28
[created_at] => 2021-12-10 10:00:56
[created_by] => 2112010001
[update_at] => 2022-01-07 21:00:19
[update_by] => 2201050001
[id_department] => 4
[nama_department] => Human Resource
[id_jabatan] => 5
[nama_jabatan] => Foreman
[status] => 4
[total_overtime] => 7
[val_overtime] => 4
[val_workexp] => 4
[val_age] => 4
[val_salary] => 4
)
)
And I just need to take some keys but keep it in the same structure. like this:
Array
(
[0] => Array
(
[val_overtime] => 4
[val_workexp] => 2
[val_age] => 2
[val_salary] => 2
)
[1] => Array
(
[val_overtime] => 4
[val_workexp] => 2
[val_age] => 4
[val_salary] => 3
)
[2] => Array
(
[val_overtime] => 4
[val_workexp] => 4
[val_age] => 4
[val_salary] => 4
)
)
How to do it?
I have tried to extract the values ??�??�manually in a loop like this:
foreach ($arr_result_all as $key => $value) {
foreach ($value as $key1 => $value1) {
}
}
But I'm stuck, I don't know what to do again in the 2nd iteration..
Please help, thank you very much for any help
Answer
Solution:
Providing a similar but slightly different solution
$newArr = array();
$targetKeys = array("val_overtime","val_workexp","val_age","val_salary");
foreach($arr_result_all as $d){
$newAssoc = array();
foreach($targetKeys as $key){
$newAssoc[$key] = $d[$key];
}
$newArr[] = $newAssoc;
}
print_r($newArr);
Source