简体   繁体   English

在PHP数组中添加键值?

[英]Adding key-value in PHP array?

Inside a loop where I'm dealing with variables related to a product and a number of units , I'm trying to add these two to an array: 在我处理与产品多个单元相关的变量的循环中,我正在尝试将这两个添加到数组中:

$pedido = array();

So, 所以,

    foreach($_POST as $post_key => $post_value){

        if ($post_value=="on") {
            $nombreProducto = mysql_fetch_assoc($mySQL->query("SELECT nombre from productos WHERE id_producto='$post_key'"));
            $cantidad = $_POST[$post_key."Number"];

            echo "<h1>".$nombreProducto['nombre']."</h1>"." Cantidad: ".$cantidad." <br><br>";
            $pedido["$nombreProducto"] = $cantidad;
        }
    }

It's right in: 它是对的:

$pedido["$nombreProducto"] = $cantidad;

Where I try to perform the adding, however the output of var_dump is like: 我尝试执行添加的地方,但var_dump的输出如下:

array(1) { ["Array"]=> string(1) "3" } array(1){[“Array”] => string(1)“3”}

Not exactly what I wanted neither the format. 不完全是我想要的格式。

Remove quotes and 删除引号和

$pedido[$nombreProducto['nombre']] = $cantidad;

EDITED EDITED

It seems $nombreProducto is an array so you need to indicate the key field, so i changed to use the field "nombre" 似乎$nombreProducto是一个数组,所以你需要指出关键字段,所以我改为使用字段“nombre”

If you see your var_dump it's an Array with the key "Array" this is why you are trying to convert the array to string and it return the word "Array" 如果你看到你的var_dump它是一个带有"Array"键的"Array"这就是为什么你试图将数组转换为字符串并返回单词"Array"

You shouldn't be putting a variable by itself in quotes. 你不应该在引号中单独放置一个变量。 Remove the quotes. 删除引号。

Also, since you were just accessing $nombreProducto['nombre'] on the previous line, it's fairly obvious that that variable is an array. 此外,由于您只是在前一行访问$nombreProducto['nombre'] ,因此很明显该变量是一个数组。 You cannot use an array as a key, only integers and strings are allowed. 您不能将数组用作键,只允许使用整数和字符串。 So use something that identifies it, such as its ID number. 因此,请使用标识它的内容,例如ID号。

Use $pedido[$nombreProducto['nombre']] = $cantidad; 使用$pedido[$nombreProducto['nombre']] = $cantidad; instead of $pedido["$nombreProducto"] = $cantidad; 而不是$pedido["$nombreProducto"] = $cantidad;

The following makes no sense to do: 以下做法没有意义:

$pedido["$nombreProducto"] = $cantidad;

What are you trying to do here? 你想在这做什么? I think what you want to do is this: 我想你想要做的是:

$pedido[$nombreProducto['nombre']] = $cantidad;

Also you may want to try to output the array like this: 您也可以尝试输出这样的数组:

print_r($pedido);

I recommend you re-write the mysql query so you don't need to loop it. 我建议你重新编写mysql查询,这样你就不需要循环了。 That is for safety and efficiency reasons. 这是出于安全和效率的原因。 I also recommend you use mysqli instead of mysql, because mysql is deprecated and unsafe to use. 我还建议您使用mysqli而不是mysql,因为不推荐使用mysql并且使用不安全。 You don't check if $_POST[$post_key."Number"] is set, at least not in this code. 您不检查是否设置了$ _POST [$ post_key。“Number”],至少不在此代码中。 I hope you sanitize/validate the input from $_POST before using it against the database? 我希望你在对数据库使用之前清理/验证来自$ _POST的输入?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM