7

I have a JSON string that looks something like this:

{"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"}]}}

What would be the PHP to decode this and place address1, address2, address3, city, and postalCode into session variables?

So far I tried this but it's not working:

$results = json_decode(strstr($address, '{"addresses":{"address":[{'), true);
$_SESSION['address1'] = $results['address']['address1'];

Thanks!

5
  • What isn't happening that you want to happen? Always describe the behavior you want and the behavior you get. Commented Aug 12, 2009 at 21:36
  • Did you call session_start() before outputting anything and before referencing $_SESSION? Commented Aug 12, 2009 at 21:39
  • 1
    So many answers... so few question upvotes. Commented Aug 12, 2009 at 21:41
  • 2
    Many answers often means easy question, not good question. Commented Aug 12, 2009 at 22:23
  • Easy for some but not others, hence the question. Commented Aug 12, 2009 at 23:59

9 Answers 9

11

print_r is your friend for figuring out JSON structure.

<?php

$addresses = json_decode('{"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"}]}}');

$_SESSION['address1'] = $addresses->addresses->address[0]->address1;
$_SESSION['address2'] = $addresses->addresses->address[0]->address2;
$_SESSION['address3'] = $addresses->addresses->address[0]->address3;
$_SESSION['city'] = $addresses->addresses->address[0]->city;
$_SESSION['postalCode'] = $addresses->addresses->address[0]->postalCode;

print_r($_SESSION);

Results in:

Array
(
    [address1] => xyz
    [address2] => 
    [address3] => 
    [city] => xyz
    [postalCode] => 111111
)
Sign up to request clarification or add additional context in comments.

2 Comments

+1 To print the results nicely as displayed, add the <pre> tag after the print_r(). This one took me forever to figure out!
AWESOME!!!! Thank you very much, this did it. I negelected to mention that I was using strstr because I had some other header information contained in $addresses and needed to chop it out. But this did the job and I'm very grateful!!!
3

json_decode will decode a json-formatted string into a PHP object.

Try this:

$results = json_decode($address);
$results['address1'] = $results->addresses->address[0]->address1;
$results['address2'] = $results->addresses->address[0]->address2;
$results['address3'] = $results->addresses->address[0]->address3;
$results['city'] = $results->addresses->address[0]->city;
$results['postalCode'] = $results->addresses->address[0]->postalCode;

Edit - updated, I misread your JSON at first.

Comments

2

Note that those "@array" and "@id" fields are invalid JSON notation, and technically they lead to unspecified behavior in JSON parsers.

Comments

1

Why not decode the whole JSON string and then get what you need?

$address = '{"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"}]}}';
$results = json_decode($address, true);
$address = $results['addresses']['address'][0];
print $address['address1'];
print $address['address2'];
print $address['postalCode'];

Comments

0

Maybe try $results['addresses']['address']['address1'];

Not sure why you're using strstr. but it doesn't look like it'd change anything in this instance.

Comments

0

you can use print_r to output the $results to find out exactly what the object output looks like.

Comments

0

If you do print_r of your array, you see how the layout is:

stdClass Object
(
  [addresses] => stdClass Object
    (
      [address] => Array
        (
          [0] => stdClass Object
            (
              [@array] => true
              [@id] => 888888
              [@uri] => xyz
              [household] => stdClass Object
                (
                  [@id] => 44444
                  [@uri] => xyz
                )

              [person] => stdClass Object
                (
                  [@id] => 
                  [@uri] => 
                )

              [addressType] => stdClass Object
                (
                  [@id] => 1
                  [@uri] => xyz
                  [name] => Primary
                )

              [address1] => xyz
              [address2] => 
              [address3] => 
              [city] => xyz
              [postalCode] => 111111
            )
        )
    )
)

Comments

0

json_decode($jsonData) returns an object btw, not an array.

For example:

stdClass Object
(
    [addresses] => stdClass Object
        (
            [address] => Array
                (
                    [0] => stdClass Object
                        (
                            [@array] => true
                            [@id] => 888888
                            [@uri] => xyz
                            [household] => stdClass Object
                                (
                                    [@id] => 44444
                                    [@uri] => xyz
                                )

                            [person] => stdClass Object
                                (
                                    [@id] => 
                                    [@uri] => 
                                )

                            [addressType] => stdClass Object
                                (
                                    [@id] => 1
                                    [@uri] => xyz
                                    [name] => Primary
                                )

                            [address1] => xyz
                            [address2] => 
                            [address3] => 
                            [city] => xyz
                            [postalCode] => 111111
                        )

                )

        )

)

Ways to access data:

$object = json_decode($jsonString);
$object->addresses->address[0]; // First address object
$object->addresses->address[0]->{"@array"}; // Not good way to access object property (damn @)
$object->addresses->address[0]->address1;
$object->addresses->address[0]->addressType->{"@id"}; // Again damn @

Comments

0

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);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.