How to access the property in an object in PHP

0

When I gave this command echo $ user-> gt; cards appeared:

[{"id":10,"id_user":2,"id_card":"11222","created_at":null,"updated_at":null,"deleted_at":null}]

How to access the 'deleted_at' property of this object?

    
asked by anonymous 27.07.2018 / 19:19

1 answer

3

When using json_decode, the string generates an object within an array. Here's how to proceed:

$str = '[{"id":10,"id_user":2,"id_card":"11222","created_at":null,"updated_at":null,"deleted_at":null}]';
   $arr = json_decode($str);
   $deleted_at = $arr[0]->deleted_at;

   if (is_null($deleted_at)) {
      echo 'Nulo';
   } else {
      echo 'Não nulo';
   }

This will display "Null".

    
27.07.2018 / 19:34