I am stuck with deleting an object inside an array. I have created a class with a couple of instances that I have subsequently placed in an array ( $arreglo
). I want to get the function eliminar()
to completely delete one of those objects from the property id
. I've tried via unset()
but from what I've read, if it's inside a function, the delete is only local. I've been searching and can't find any solution.
So there isn't any to delete the object directly inside the foreach?
<?php
class Producto {
public $id;
public $nombre;
public $descripcion;
public $cantidad_stock;
public $precio;
public function __construct($id, $nombre, $descripcion, $cantidad_stock, $precio) {
$this->id = $id;
$this->nombre = $nombre;
$this->descripcion = $descripcion;
$this->cantidad_stock = $cantidad_stock;
$this->precio = $precio;
}
public function get_id() {
return $this->id;
}
public function set_id($x) {
$this->id = $x;
}
public function get_nombre() {
return $this->nombre;
}
public function set_nombre($x) {
$this->nombre = $x;
}
public function get_descripcion() {
return $this->descripcion;
}
public function set_descripcion($x) {
$this->descripcion = $x;
}
public function get_cantidad_stock() {
return $this->cantidad_stock;
}
public function set_cantidad_stock($x) {
$this->cantidad_stock = $x;
}
public function get_precio() {
return $this->precio;
}
public function set_precio($x) {
$this->precio = $x;
}
}
$arreglo = [new Producto(1, "avellanas", "frutos secos", 25, 15.12), new Producto(2, "bacalao", "pescado", 12, 25)];
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="id" placeholder="id">
<input type="text" name="nombre" placeholder="nombre">
<input type="text" name="descripcion" placeholder="descripcion">
<input type="text" name="stock" placeholder="cantidad en stock">
<input type="text" name="precio" placeholder="precio">
<input type="submit" value="enviar">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['id'])) {
$id = intval($_POST['id']);
$nombre = $_POST['nombre'];
$descripcion = $_POST['descripcion'];
$stock = intval($_POST['stock']);
$precio = floatval($_POST['precio']);
array_push($arreglo, new Producto($id, $nombre, $descripcion, $stock, $precio));
}
function mostrar($x) {
foreach($x as $y) {
$data = "Id: $y->id\nNombre: $y->nombre\nDescripción: $y->descripcion\nQuedan en Stock: $y->cantidad_stock\nPrecio: $y->precio\n<br>";
echo $data;
}
}
function datos($x,$y) {
foreach($y as $obj) {
if($obj->id == $x) {
echo "Id: $obj->id\nNombre: $obj->nombre\nDescripción: $obj->descripcion\nQuedan en Stock: $obj->cantidad_stock\nPrecio: $obj->precio\n<br>";
}
}
}
function setting($id, $name, $desc, $price, $caja) {
foreach($caja as $obj) {
if($id == $obj->id){
$obj->set_nombre($name);
$obj->set_descripcion($desc);
$obj->set_precio($price);
}
}
}
function eliminar($id, $caja) {
foreach($caja as $obj){
if($id == $obj->id){
unset($obj);
}
}
}
eliminar(2, $arreglo);
datos(2, $arreglo);
You can use pass by reference and remove from the array from the function an array key, for example:
Documentation: https://www.php.net/manual/en/language.references.pass.php
As you have been told, you could pass the
array
to the function by referenceeliminar()
, so that the modifications that it suffers within the function are reflected outside of it.Another option would be to return the
array
updated one and crush the one you had before calling the function.As you have read, if you use unset inside a function, what you will do is modify the local instance of the variable (unless you use $GLOBALS ). To do what you want, you must either declare that the function variable is going to be treated as a reference as the fellow @Xerif recommended, or modify the global version of the variable, or return a new variable in your function that replaces the old one. global. as the fellow @phpMyGuel recommended.
One possible solution, among the many that exist, is that instead of using
unset
, use array_splice to remove a specific index from the array if the object at that index contains theid
one you want to remove:Declare the function variable as a variable passed by reference:
Snippet in Ideone
Modifying the global variable inside your function:
Snippet in Ideone
Returning a new variable to replace the global one:
Snippet in Ideone