How to access the property in a PHP object

Asked

Viewed 371 times

0

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

[{"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 ?

1 answer

3


When using json_decode, the string generates an Object within an array. See below 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".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.