Postingan lainnya
Merubah Bentuk data array
Assalamualaikum,
Maaf temen-temen semua mungkin disini ada yang paham bagaiman cara merubah bentuk array... Saya ada data seperti ini :
<?php
$data_contoh = array(
array(
'tahun' => 2018,
'bulan' => 1,
'nyeri' => 35,
'jatuh' => 19,
'dekubitus' => 9,
'nilai_budaya' => 20,
'sosial' => 22,
'spiritual' => 30,
'psikologi' => 25,
'disscharge_planning' => 17,
'riwayat_alergi' => 27,
'edukasi_terintegrasi' => 8,
'skrining_gizi' => 36,
'kajian_status' => 27),
array(
'tahun' => 2018,
'bulan' => 2,
'nyeri' => 30,
'jatuh' => 19,
'dekubitus' => 19,
'nilai_budaya' => 26,
'sosial' => 22,
'spiritual' => 30,
'psikologi' => 20,
'disscharge_planning' => 17,
'riwayat_alergi' => 27,
'edukasi_terintegrasi' => 8,
'skrining_gizi' => 33,
'kajian_status' => 23),
array(
'tahun' => 2018,
'bulan' => 3,
'nyeri' => 35,
'jatuh' => 1,
'dekubitus' => 19,
'nilai_budaya' => 20,
'sosial' => 22,
'spiritual' => 30,
'psikologi' => 15,
'disscharge_planning' => 17,
'riwayat_alergi' => 27,
'edukasi_terintegrasi' => 18,
'skrining_gizi' => 23,
'kajian_status' => 33),
);
?>
Dan saya ingin menjadikan bentuk array diatas menjadi seperti ini :
<?php
$data_hasil_akhir = array( 'nyeri' =>
array(
'2018_1' => 35,
'2018_2' => 30,
'2018_3' => 35
),
'jatuh' =>
array(
'2018_1' => 19,
'2018_2' => 19,
'2018_3' => 1
),
'dekubitus' =>
array(
'2018_1' => 19,
'2018_2' => 19,
'2018_3' => 9
),
'nilai_budaya' =>
array(
'2018_1' => 20,
'2018_2' => 26,
'2018_3' => 20
),
'sosial' =>
array(
'2018_1' => 22,
'2018_2' => 22,
'2018_3' => 22
),
'spiritual' =>
array(
'2018_1' => 30,
'2018_2' => 30,
'2018_3' => 30
),
'psikologi' =>
array(
'2018_1' => 25,
'2018_2' => 20,
'2018_3' => 15
),
'disscharge_planning' =>
array(
'2018_1' => 17,
'2018_2' => 17,
'2018_3' => 17
),
'riwayat_alergi' =>
array(
'2018_1' => 27,
'2018_2' => 27,
'2018_3' => 27
),
'edukasi_terintegrasi' =>
array(
'2018_1' => 18,
'2018_2' => 8,
'2018_3' => 8
),
'skrining_gizi' =>
array(
'2018_1' => 36,
'2018_2' => 33,
'2018_3' => 23
),
'kajian_status' =>
array(
'2018_1' => 27,
'2018_2' => 23,
'2018_3' => 33
)
);
?>
Nah proses di phpnya seperti apa ya?... Terimakasih, Salam
0
5 Jawaban:
Jawaban Terpilih
semoga membantu
$not = array('tahun','bulan');
foreach ($data_contoh as $key => $value) {
foreach ($value as $keyy => $val) {
//ambil data selain tahun dan bulan
if (in_array($keyy, $not) === false) {
$aa_data[$keyy][$value["tahun"]."_".$value["bulan"]] = $value[$keyy];
}
}
}
0
Terimakasih atas bantuanya @dianarifr, Salam kenal.
0
Maaf Bang saya ada kendala lagi... Minta bantuanya lagi bagaimana logikanya supaya datanya tidak berulang-ulang..
<?php
$not = array('tahun','bulan');
foreach ($data_contoh as $key => $value) {
foreach ($value as $keyy => $val) {
//ambil data selain tahun dan bulan
if (in_array($keyy, $not) === false) {
$aa_data[$keyy][$value["tahun"]."_".$value["bulan"]] = $value[$keyy] ? $value[$keyy] : 0 ;
$ThnBln[$value["tahun"]."_".$value["bulan"]] = date('m Y',strtotime($value['tahun'].'-'.$value['bulan']));
$data_chart_2[] = '{name:"'.$NamaKategori2[$keyy].'",data:['.implode(' , ',$aa_data[$keyy]).']}';
}
}
}
//ini jika di print_r($data_chart_2); maka dia berulang seperti ini
Array
(
[0] => {name:"Nyeri",data:[35]}
[1] => {name:"Jatuh",data:[19]}
[2] => {name:"Dekubitus",data:[9]}
[3] => {name:"Nilai Budaya",data:[20]}
[4] => {name:"Sosial",data:[22]}
[5] => {name:"Spiritual",data:[30]}
[6] => {name:"Psikologi",data:[25]}
[7] => {name:"Discharge Planing",data:[17]}
[8] => {name:"Riwayat Alergi",data:[27]}
[9] => {name:"Edukasi Terintegrasi",data:[8]}
[10] => {name:"Skrining Gizi",data:[36]}
[11] => {name:"Pengkajian <8 Jam",data:[27]}
[12] => {name:"Nyeri",data:[35 , 30]}
[13] => {name:"Jatuh",data:[19 , 19]}
[14] => {name:"Dekubitus",data:[9 , 19]}
[15] => {name:"Nilai Budaya",data:[20 , 26]}
[16] => {name:"Sosial",data:[22 , 22]}
[17] => {name:"Spiritual",data:[30 , 30]}
[18] => {name:"Psikologi",data:[25 , 20]}
[19] => {name:"Discharge Planing",data:[17 , 17]}
[20] => {name:"Riwayat Alergi",data:[27 , 27]}
[21] => {name:"Edukasi Terintegrasi",data:[8 , 8]}
[22] => {name:"Skrining Gizi",data:[36 , 33]}
[23] => {name:"Pengkajian <8 Jam",data:[27 , 23]}
[24] => {name:"Nyeri",data:[35 , 30 , 35]}
[25] => {name:"Jatuh",data:[19 , 19 , 1]}
[26] => {name:"Dekubitus",data:[9 , 19 , 19]}
[27] => {name:"Nilai Budaya",data:[20 , 26 , 20]}
[28] => {name:"Sosial",data:[22 , 22 , 22]}
[29] => {name:"Spiritual",data:[30 , 30 , 30]}
[30] => {name:"Psikologi",data:[25 , 20 , 15]}
[31] => {name:"Discharge Planing",data:[17 , 17 , 17]}
[32] => {name:"Riwayat Alergi",data:[27 , 27 , 27]}
[33] => {name:"Edukasi Terintegrasi",data:[8 , 8 , 18]}
[34] => {name:"Skrining Gizi",data:[36 , 33 , 23]}
[35] => {name:"Pengkajian <8 Jam",data:[27 , 23 , 33]}
)
?>
bagaimana solusinya ya?..
0
format data yang akan di tampilkan di $data_chart_2 gimana ya?
0
Alhamdulillah udah ketemu bang, maaf baru dikabarin... Terimakasih banyak atas bantuanya... Salam
0