如何更新数组的值?我有一个如下数组:
Array ( [Sucursal1] => Array (
[SucursalNombre] => Sucursal1
[Producto1] => 225
[Producto2] => 2250
[Producto3] => 22
[Producto4] => 40
[Producto5] => 20
)
[Sucursal2] => Array (
[SucursalNombre] => Sucursal2
[Producto1] => 100
[Producto2] => 250
[Producto3] => 221
[Producto4] => 404
[Producto5] => 202
))
我想为 [branch1] 更新 [Product1] => 50 的值,即
Array ( [Sucursal1] => Array (
[SucursalNombre] => Sucursal1
[Producto1] => **50**
[Producto2] => 2250
[Producto3] => 22
[Producto4] => 40
[Producto5] => 20
)
[Sucursal2] => Array (
[SucursalNombre] => Sucursal2
[Producto1] => 100
[Producto2] => 250
[Producto3] => 221
[Producto4] => 404
[Producto5] => 202
))
考虑到它是一个数组数组,假设您将它包含在一个名为 的变量中
$array
,我们要做的第一件事就是访问第一个分支。为此,我们可以使用:
完成后,我们可以获得 Product1 的值,如下所示:
以一种简化的方式访问此产品将是:
因此,要为其分配一个新值,我们可以这样做:
您拥有的是以下多维数组:
要进行所需的更改,您必须访问第一个数组的索引,然后访问要修改其值的索引: