Issues with array_push() when pushing array to array

55 Views Asked by At

I have data that I want to sort.

{
"0": {
    "jenis_cairan_masuk": {
        "name": "jenis_cairan_masuk",
        "value": "Oral (Enteral)"
    },
    "keterangan_cairan_masuk": {
        "name": "keterangan_cairan_masuk",
        "value": "langsung"
    },
    "jumlah_cairan_masuk": {
        "name": "jumlah_cairan_masuk",
        "value": "28"
    }
},
"1": {
    "jenis_cairan_masuk": {
        "name": "jenis_cairan_masuk",
        "value": "Injeksi"
    },
    "keterangan_cairan_masuk": {
        "name": "keterangan_cairan_masuk",
        "value": "langsung"
    },
    "jumlah_cairan_masuk": {
        "name": "jumlah_cairan_masuk",
        "value": "50"
    }
},
"2": {
    "jenis_cairan_masuk": {
        "name": "jenis_cairan_masuk",
        "value": "Injeksi"
    },
    "keterangan_cairan_masuk": {
        "name": "keterangan_cairan_masuk",
        "value": "tidak langsung"
    },
    "jumlah_cairan_masuk": {
        "name": "jumlah_cairan_masuk",
        "value": "10"
    }
}
}

The data is a string and was declared in the $data['tabel_cairan_masuk'] variable. I want to place every data in the object to provide an array. Here is my code.

$tabel_cairan_masuk = json_decode($data['tabel_cairan_masuk']);
$jenis_cairan_masuk_master = [
    'Infus' => [],
    'Injeksi' => [],
    'Transfusi' => [],
    'Oral (Enteral)' => []
];

foreach($tabel_cairan_masuk AS $key2 => $value2){
    $jenis_cairan_masuk = $value2->jenis_cairan_masuk->value;
    $ket_cairan_masuk = $value2->keterangan_cairan_masuk->value;
    $jumlah_cairan_masuk = $value2->jumlah_cairan_masuk->value;
    foreach($jenis_cairan_masuk_master AS $key3 => $value3){
        if($jenis_cairan_masuk == $key3){
            array_push($value3, [$jenis_cairan_masuk, $ket_cairan_masuk, $jumlah_cairan_masuk]);
        }
    }
};

var_dump($jenis_cairan_masuk_master);

The code above will decode the raw object string data to a real object so that it can be looped. After that, I declared an array to facilitate the desired sorting format ($jenis_cairan_masuk_master). The problem exists on the array_push(), where it seems not updating the array provided, so when I check using var_dump(), it returns empty, like this:

array(4) { ["Infus"]=> array(0) { } ["Injeksi"]=> array(0) { } ["Transfusi"]=> array(0) { } ["Oral (Enteral)"]=> array(0) { } } array(4) { ["Infus"]=> array(0) { } ["Injeksi"]=> array(0) { } ["Transfusi"]=> array(0) { } ["Oral (Enteral)"]=> array(0) { } } array(4) { ["Infus"]=> array(0) { } ["Injeksi"]=> array(0) { } ["Transfusi"]=> array(0) { } ["Oral (Enteral)"]=> array(0) { } }

Why is that, and what is the solution?

1

There are 1 best solutions below

1
Karl Hill On BEST ANSWER

The issue is that arrays are passed by value, not by reference, in PHP. This means that when looping over $jenis_cairan_masuk_master with foreach, the $value3 variable is a copy of the original array, not a reference. So when using array_push() on $value3, you're modifying the copy, not the original array.

You can use the & operator to pass the array by reference to modify the original array.

foreach($tabel_cairan_masuk AS $key2 => $value2){
    $jenis_cairan_masuk = $value2->jenis_cairan_masuk->value;
    $ket_cairan_masuk = $value2->keterangan_cairan_masuk->value;
    $jumlah_cairan_masuk = $value2->jumlah_cairan_masuk->value;
    foreach($jenis_cairan_masuk_master AS $key3 => &$value3){
        if($jenis_cairan_masuk == $key3){
            array_push($value3, [$jenis_cairan_masuk, $ket_cairan_masuk, $jumlah_cairan_masuk]);
        }
    }
};

In this code, foreach($jenis_cairan_masuk_master AS $key3 => &$value3) passes $value3 by reference, so any modifications to $value3 will affect the original array.