How to decode a JSON string in PHP?

后端 未结 9 1155
挽巷
挽巷 2020-12-02 02:18

I have a JSON string that looks something like this:

{\"addresses\":{\"address\":[{\"@array\":\"true\",\"@id\":\"888888\",\"@uri\":\"xyz\",\"household\":{\"@         


        
9条回答
  •  天涯浪人
    2020-12-02 02:43

    This one will put all scalar and null values into session where key does not begin with a @

    $jsonString = '{"addresses":{"address":[{"@array":"true","@id":"888888","@uri":"xyz","household":{"@id":"44444","@uri":"xyz"},"person":{"@id":"","@uri":""},"addressType":{"@id":"1","@uri":"xyz","name":"Primary"},"address1":"xyz","address2":null,"address3":null,"city":"xyz","postalCode":"111111"}]}}';
    
    $result = json_decode($jsonString);
    
    // will put *all* scalar and null values into session where key does not begin with a @
    foreach($result->addresses->address[0] as $key=>$value) {
        if (substr($key, 0, 1) != '@'  && (is_scalar($value) || is_null($value)) ) {
            $_SESSION[$key] = $value;
        } 
    }
    
    print_r($_SESSION);
    
    ?>
    

提交回复
热议问题