Remove object from a PHP array

0
{
    "success": true,
    "response": [
        {
            "idseparacion": "1",
            "idcalidad": "21",
            "peso": "500",
            "cajas": "50",
            "restante": 100
        },
        {
            "idseparacion": "2",
            "idcalidad": "21",
            "peso": "1000",
            "cajas": "50",
            "restante": 0
            },
    ],
}

I have my json as it is in the format the problem is that I want to delete the elements that have restante 0 use:

unset($data[$key]);

which eliminates the data to me well, the problem is that I add them in quotes the number of object remaining like this:

"response": [
        "1":{ ///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            "idseparacion": "1",
            "idcalidad": "21",
            "peso": "500",
            "cajas": "50",
            "restante": 100
        }
    ],
  

Why is this happening?

That another way I recommend to remove objects

  

PHP Code

foreach($data as $key => $value){
                $cajasSeparacion = $this->getCajasCalidad($value['idseparacion']);
                $cajasTarimadas = $this->getTarimasCajas($value['idseparacion']);
                $valorRestante = $cajasSeparacion->cajas - $cajasTarimadas->tarimadas;
                $data[$key]['restante'] = $valorRestante;
                if($valorRestante <= 0){
                    unset($data[$key]);
                }
            }
    
asked by DoubleM 12.11.2018 в 21:01
source

1 answer

0

I would recommend not to manipulate the objects too much, unless you have no other way out.

In this case you can treat your object as a JSON during the whole journey, avoiding an array conversion that does not really contribute anything to you.

For example, to do what you say in the question, this would suffice:

/*Evitamos TRUE para dejarlo como JSON*/
$data=json_decode($str);

foreach($data->response as $k=>$item){
    if ($item->restante <= 0){
        unset($data->response[$k]);
    }
}

If we now try the data:

var_dump($data);

Exit:

object(stdClass)#1 (2) {
  ["success"]=>
  bool(true)
  ["response"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (5) {
      ["idseparacion"]=>
      string(1) "1"
      ["idcalidad"]=>
      string(2) "21"
      ["peso"]=>
      string(3) "500"
      ["cajas"]=>
      string(2) "50"
      ["restante"]=>
      int(100)
    }
  }
}

If for some reason you want to work as an array, no problem , you can do it quietly:

/*Pasamos TRUE para convertirlo a array*/
$arr=json_decode($str,TRUE);

foreach ($arr["response"] as $k=>$response){
    if ($response["restante"] <= 0){
    unset($arr["response"][$k]);
    }
}

We test the resulting array:

var_dump($arr);

Exit:

array(2) {
  ["success"]=>
  bool(true)
  ["response"]=>
  array(1) {
    [0]=>
    array(5) {
      ["idseparacion"]=>
      string(1) "1"
      ["idcalidad"]=>
      string(2) "21"
      ["peso"]=>
      string(3) "500"
      ["cajas"]=>
      string(2) "50"
      ["restante"]=>
      int(100)
    }
  }
}
    
answered by 12.11.2018 в 23:58