0

How to decode and store the following code values like city, zip, region etc into separate variables in PHP

a:14:{s:4:"city";s:7:"Bijapur";s:3:"zip";s:6:"586101";s:3:"lat";d:16.83329963684082;s:2:"as";s:33:"AS9829 National Internet Backbone";s:5:"query";s:13:"59.99.192.232";s:6:"region";s:2:"KA";s:10:"regionName";s:9:"Karnataka";s:3:"org";s:4:"BSNL";s:3:"isp";s:4:"BSNL";s:7:"country";s:5:"India";s:11:"countryCode";s:2:"IN";s:3:"lon";d:75.69999694824219;s:6:"status";s:7:"success";s:8:"timezone";s:12:"Asia/Kolkata";}

Edited by blueguy777: programming language mentioned

3
Contributors
3
Replies
17
Views
2 Years
Discussion Span
Last Post by blueguy777
0

From where are you getting this serialized data? Hope it's not stored that way in a DB? Anyhow:

<?php
$names = ['city','zip','lat','as','query','region','regionName','org','isp','country','countryCode','lon','status','timezone'];
$ser = 'a:14:{s:4:"city";s:7:"Bijapur";s:3:"zip";s:6:"586101";s:3:"lat";d:16.83329963684082;s:2:"as";s:33:"AS9829 National Internet Backbone";s:5:"query";s:13:"59.99.192.232";s:6:"region";s:2:"KA";s:10:"regionName";s:9:"Karnataka";s:3:"org";s:4:"BSNL";s:3:"isp";s:4:"BSNL";s:7:"country";s:5:"India";s:11:"countryCode";s:2:"IN";s:3:"lon";d:75.69999694824219;s:6:"status";s:7:"success";s:8:"timezone";s:12:"Asia/Kolkata";}';
//Creates and array
$arr = unserialize($ser);

//Loop over array to see all items stored as expected
echo "<br /><br />LOOP OVER \$array METHOD<br />=======================<br />";
foreach($arr as $key=>$value)
   echo "ARRAY[$key]" . ' = ' . $value . '<br />';

//You can use extract to get individual variables. BUT be careful. Useful to set a custom prefix to ALL vars
echo "<br /><br />USE EXTRACT METHOD on ARRAY<br />============================<br />";
extract($arr,EXTR_PREFIX_ALL,'u');
foreach($names as $name) {
    $nm = 'u_' . $name;
    echo $nm . ' = ' . $$nm . '<br />';
}

//You could also use list(), but that only works on number-indexed arrays, not keyed arrays. However, it could be done thus:
echo "<br /><br />USE LIST METHOD on ARRAY<br />========================<br />";
list($city,$zip,$lat,$as,$query,$region,$regionName,$org,$isp,$country,$countryCode,$lon,$status,$timezone) = array_values($arr);
foreach($names as $name)
    echo $name . ' = ' . $$name . '<br />';

Edited by diafol

0

This appears to be a PHP serialized string. If that's your general usecase, then the simplest way to decode it is via the unserialize() function, like so:

$serialized_string = 'a:14:{s:4:"city";s:7:"Bijapur";s:3:"zip";s:6:"586101";s:3:"lat";d:16.83329963684082;s:2:"as";s:33:"AS9829 National Internet Backbone";s:5:"query";s:13:"59.99.192.232";s:6:"region";s:2:"KA";s:10:"regionName";s:9:"Karnataka";s:3:"org";s:4:"BSNL";s:3:"isp";s:4:"BSNL";s:7:"country";s:5:"India";s:11:"countryCode";s:2:"IN";s:3:"lon";d:75.69999694824219;s:6:"status";s:7:"success";s:8:"timezone";s:12:"Asia/Kolkata";}';

$unserialized_array = unserialize($serialized_string);

foreach ($unserialized_array as $key=>$value) {
      print "$key: $value\n";
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.