-3

Hello I am trying to process and read/parse data from a string in PHP. The string when var dumped looks like this

 string(266) "{ "address": { "address_line1": "2391 US HIGHWAY 22 W", "address_line2": "", "address_city": "UNION", "address_state": "NJ", "address_zip": "07083-8517", "address_country": "US", "object": "address" } }" 

I would like to be able to store

address_line1, address_line2, address_city, address_state, address_zip and address_country into variables so I can manipulate them as I please.

How do I go about doing this.

Thank you for your time.

pnuts
  • 58,317
  • 11
  • 87
  • 139
user1863457
  • 131
  • 2
  • 11

2 Answers2

1

You have to use json_decode() function to retrieve your data:

$input = '{ "address": { "address_line1": "2391 US HIGHWAY 22 W", "address_line2": "", "address_city": "UNION", "address_state": "NJ", "address_zip": "07083-8517", "address_country": "US", "object": "address" } }';
$result = json_decode($input, true);

var_dump($result); // see whole array

echo $result['address']['address_line1']; // specific data

More information you can get from manual.

marian0
  • 3,336
  • 3
  • 27
  • 37
0

You can convert the string to JSON using the following instruction

$obj = json_decode($json);

Then you can get the values from the JSON as below

$obj->address->address_line1;
Vishnu
  • 11,614
  • 6
  • 51
  • 90