English 中文(简体)
在将阵列推向阵列时与阵列有关的问题
原标题:Issues with array_push() when pushing array to array

我有我想分类的数据。

{
"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"
    }
}
}

这些数据是细微的,在数据[tabel_cairan_masuk]变量中公布。 我想把所有数据放在标语中,以便提供一个阵列。 这里是我的法典。

$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);

以上代码将原始物体数据编码为真实物体,以便其能够使用。 之后,我宣布了一个阵列,以便利理想的分类格式(jenis_cairan_masuk_master)。 阵列(push)(似乎没有更新所提供的阵列)存在问题,因此,当我用al_dump(var)检查时,它就如:

[0]

为什么如此,解决办法是什么?

问题回答

问题是,阵列在公用住房价格中以价值而不是参照方式通过。 这意味着,在超过<条码>时,。 <代码> 价值3变量为原始阵列的拷贝,而不是参考。 因此,在使用<<代码>-push()>时,请将复制件改为原阵列。

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]);
        }
    }
};

在这项法典中,foreach($jenis_cairan_masuk_master AS$key3 => &$ Value3)%20$3,以提及方式对$3的任何修改都将影响到原始阵列。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...