How can I update the value of an array? I have an array like the following:
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
))
I want to Update the value of [Product1] => 50 for [branch1] ie
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
))
Taking into account that it is an array of arrays, and let's say that you have it contained in a variable called
$array
, the first thing we will have to do is access the first branch.To do this, we can use:
Once this is done, we can obtain the value of Product1 as follows:
In a simplified way to access this product it would be:
Therefore, to assign it a new value we could do:
What you have is the following multidimensional array:
To make the change you want, you have to access the index of the first array and then its index whose value you are going to modify: