Jump to content

How to parse a value into several variables


refiking

Recommended Posts

I am trying to take a value that is stored in a db for address and parse it into 4 different variables:

$address

$city

$state

$zip

 

when I started the loop and echo'd the results, here is how it comes out:

a:4:{s:14:"street_address";s:20:"811 East Parrish Ave";s:4:"city";s:9:"Owensboro";s:5:"state";s:2:"KY";s:3:"zip";s:5:"42303";}
a:4:{s:14:"street_address";b:0;s:4:"city";b:0;s:5:"state";b:0;s:3:"zip";b:0;}
a:4:{s:14:"street_address";s:15:"800 Rose Street";s:4:"city";s:9:"Lexington";s:5:"state";s:2:"KY";s:3:"zip";s:5:"40536";}
a:4:{s:14:"street_address";s:19:"1300 State Hwy 3298";s:4:"city";s:10:"Olive Hill";s:5:"state";s:2:"KY";s:3:"zip";s:5:"41164";}
a:4:{s:14:"street_address";s:22:"1301 North Race Street";s:4:"city";s:7:"Glasgow";s:5:"state";s:2:"KY";s:3:"zip";s:5:"42141";}
a:4:{s:14:"street_address";s:23:"200 Abraham Flexner Way";s:4:"city";s:10:"Louisville";s:5:"state";s:2:"KY";s:3:"zip";s:5:"40202";}

That data is a serialized array. Simply unserialize() each record and you will have an array that you can access each piece of data independently.

 

while($row = mysql_fetch_assoc($result))
{
    $data = unserialize($row['field_name']);
    echo "Street: {$data['street_address']}<br>\n";
    echo "City: {$data['city']}<br>\n";
    echo "State: {$data['state']}<br>\n";
    echo "ZIP: {$data['zip']}<br>\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.