jbradley04 Posted July 11, 2012 Share Posted July 11, 2012 Hello, I have some data that I am getting from another website. When the value is returned to me I get this data. object(stdClass)#2 (1) { ["VerifyAddressResult"]=> object(stdClass)#3 (10) { ["CityName"]=> string(10) "WASHINGTON" ["Country"]=> string(3) "USA" ["County"]=> string(20) "DISTRICT OF COLUMBIA" ["FirmNameOrRecipient"]=> string(5) "CDYNE" ["PrimaryAddressLine"]=> string(24) "1600 PENNSYLVANIA AVE NW" ["ReturnCode"]=> int(103) ["SecondaryAddressLine"]=> string(0) "" ["StateAbbreviation"]=> string(2) "DC" ["Urbanization"]=> string(0) "" ["ZipCode"]=> string(10) "20500-0003" } } How do I extract the ZipCode out of this? These results are in a $result variable and I am getting this info by a print_r($result). Any help would be greatly appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/ Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 Learn to read print_r() and var_dump() output. object(stdClass)#2 (1) { ... } is an object (of type stdClass which is just a generic class) with 1 variable. ["VerifyAddressResult"] => object(stdClass)#3 (10) { ... } The previous thing (array or object) has a "VerifyAddressResult" member which has a stdClass value. ["ZipCode"] => string(10) "20500-0003" The previous thing (array or object) has a "ZipCode" member which has a string value (with 10 characters) of "20500-0003". So $result->VerifyAddressResult->ZipCode Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1360841 Share on other sites More sharing options...
jbradley04 Posted July 11, 2012 Author Share Posted July 11, 2012 Awesome! Thanks for your help! STill trying to learn objects Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1360892 Share on other sites More sharing options...
jbradley04 Posted July 18, 2012 Author Share Posted July 18, 2012 Wow! So I thought I understood but I DO NOT! When I do this through Google's API I get these results. object(stdClass)#1 (2) { ["results"]=> array(1) { [0]=> object(stdClass)#2 (4) { ["address_components"]=> array(7) { [0]=> object(stdClass)#3 (3) { ["long_name"]=> string(4) "1600" ["short_name"]=> string(4) "1600" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [1]=> object(stdClass)#4 (3) { ["long_name"]=> string(17) "Amphitheatre Pkwy" ["short_name"]=> string(17) "Amphitheatre Pkwy" ["types"]=> array(1) { [0]=> string(5) "route" } } [2]=> object(stdClass)#5 (3) { ["long_name"]=> string(13) "Mountain View" ["short_name"]=> string(13) "Mountain View" ["types"]=> array(2) { [0]=> string( "locality" [1]=> string(9) "political" } } [3]=> object(stdClass)#6 (3) { ["long_name"]=> string(11) "Santa Clara" ["short_name"]=> string(11) "Santa Clara" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [4]=> object(stdClass)#7 (3) { ["long_name"]=> string(10) "California" ["short_name"]=> string(2) "CA" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [5]=> object(stdClass)#8 (3) { ["long_name"]=> string(13) "United States" ["short_name"]=> string(2) "US" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [6]=> object(stdClass)#9 (3) { ["long_name"]=> string(5) "94043" ["short_name"]=> string(5) "94043" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["formatted_address"]=> string(52) "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA" ["geometry"]=> object(stdClass)#10 (3) { ["location"]=> object(stdClass)#11 (2) { ["lat"]=> float(37.4231054) ["lng"]=> float(-122.0823988) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> object(stdClass)#12 (2) { ["northeast"]=> object(stdClass)#13 (2) { ["lat"]=> float(37.424454380292) ["lng"]=> float(-122.08104981971) } ["southwest"]=> object(stdClass)#14 (2) { ["lat"]=> float(37.421756419709) ["lng"]=> float(-122.08374778029) } } } ["types"]=> array(1) { [0]=> string(14) "street_address" } } } ["status"]=> string(2) "OK" } But I cannot Figure out how to get the zip code from this! Can someone give me an idea. I had $zip = $results->address_components->postal_code; but no Bueno! I GREATLY appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362471 Share on other sites More sharing options...
jbradley04 Posted July 18, 2012 Author Share Posted July 18, 2012 This may be a better and cleaner output rather than the other long output! { "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy", "short_name" : "Amphitheatre Pkwy", "types" : [ "route" ] }, { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara", "short_name" : "Santa Clara", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "94043", "short_name" : "94043", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.42291810, "lng" : -122.08542120 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.42426708029149, "lng" : -122.0840722197085 }, "southwest" : { "lat" : 37.42156911970850, "lng" : -122.0867701802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" } Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362477 Share on other sites More sharing options...
xyph Posted July 18, 2012 Share Posted July 18, 2012 That's JSON, and PHP has a parser for that <?php $string = '{ "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy", "short_name" : "Amphitheatre Pkwy", "types" : [ "route" ] }, { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara", "short_name" : "Santa Clara", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "94043", "short_name" : "94043", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.42291810, "lng" : -122.08542120 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.42426708029149, "lng" : -122.0840722197085 }, "southwest" : { "lat" : 37.42156911970850, "lng" : -122.0867701802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" }'; $json = json_decode($string); print_r($json); echo $json->results[0]->address_components[6]->long_name; ?> output stdClass Object ( [results] => Array ( [0] => stdClass Object ( [address_components] => Array ( [0] => stdClass Object ( [long_name] => 1600 [short_name] => 1600 [types] => Array ( [0] => street_number ) ) [1] => stdClass Object ( [long_name] => Amphitheatre Pkwy [short_name] => Amphitheatre Pkwy [types] => Array ( [0] => route ) ) [2] => stdClass Object ( [long_name] => Mountain View [short_name] => Mountain View [types] => Array ( [0] => locality [1] => political ) ) [3] => stdClass Object ( [long_name] => Santa Clara [short_name] => Santa Clara [types] => Array ( [0] => administrative_area_level_2 [1] => political ) ) [4] => stdClass Object ( [long_name] => California [short_name] => CA [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [5] => stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) [6] => stdClass Object ( [long_name] => 94043 [short_name] => 94043 [types] => Array ( [0] => postal_code ) ) ) [formatted_address] => 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA [geometry] => stdClass Object ( [location] => stdClass Object ( [lat] => 37.4229181 [lng] => -122.0854212 ) [location_type] => ROOFTOP [viewport] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 37.424267080291 [lng] => -122.08407221971 ) [southwest] => stdClass Object ( [lat] => 37.421569119709 [lng] => -122.08677018029 ) ) ) [types] => Array ( [0] => street_address ) ) ) [status] => OK ) 94043 Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362480 Share on other sites More sharing options...
jbradley04 Posted July 18, 2012 Author Share Posted July 18, 2012 Thank you so much! I do have a question though. For some addresses the 6th element in that array is not always the Zipcode. Is there a way to do it by name? like postal_code? That way I am sure to get the zip code everytime. Otherwise, is there another way to do it? I REALLY appreciate all this help! Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362512 Share on other sites More sharing options...
xyph Posted July 18, 2012 Share Posted July 18, 2012 Yes, you gotta loop through each component, and check if 'postal code' exists in 'types' Here's a start <?php $string = '{ "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy", "short_name" : "Amphitheatre Pkwy", "types" : [ "route" ] }, { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara", "short_name" : "Santa Clara", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "94043", "short_name" : "94043", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.42291810, "lng" : -122.08542120 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.42426708029149, "lng" : -122.0840722197085 }, "southwest" : { "lat" : 37.42156911970850, "lng" : -122.0867701802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" }'; $json = json_decode($string); foreach( $json->results[0]->address_components as $component ) { print_r($component); } ?> outputs stdClass Object ( [long_name] => 1600 [short_name] => 1600 [types] => Array ( [0] => street_number ) ) stdClass Object ( [long_name] => Amphitheatre Pkwy [short_name] => Amphitheatre Pkwy [types] => Array ( [0] => route ) ) stdClass Object ( [long_name] => Mountain View [short_name] => Mountain View [types] => Array ( [0] => locality [1] => political ) ) stdClass Object ( [long_name] => Santa Clara [short_name] => Santa Clara [types] => Array ( [0] => administrative_area_level_2 [1] => political ) ) stdClass Object ( [long_name] => California [short_name] => CA [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) stdClass Object ( [long_name] => 94043 [short_name] => 94043 [types] => Array ( [0] => postal_code ) ) Using in_array on $component->type might be helpful here Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362517 Share on other sites More sharing options...
jbradley04 Posted July 18, 2012 Author Share Posted July 18, 2012 I am in way in over my head here! How would I check if postal_code is present? $component->type->postal_code Also, how would is_array be helpful? I looked it up on php.net but I just do not understand how that would play into this Sorry to be so needy... Thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362578 Share on other sites More sharing options...
xyph Posted July 18, 2012 Share Posted July 18, 2012 $component->type isn't an object, it's an array. Does that clear it up a bit? Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362589 Share on other sites More sharing options...
jbradley04 Posted July 18, 2012 Author Share Posted July 18, 2012 I am embarrased to admit but it doesnt. I am so lost with these arrays and objects. I cant tell the difference! :'( Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362599 Share on other sites More sharing options...
xyph Posted July 18, 2012 Share Posted July 18, 2012 Well, I don't think handing the solution to you would be the best way to do this, but I don't have the time to teach right now <?php $string = '{ "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy", "short_name" : "Amphitheatre Pkwy", "types" : [ "route" ] }, { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara", "short_name" : "Santa Clara", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "94043", "short_name" : "94043", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.42291810, "lng" : -122.08542120 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.42426708029149, "lng" : -122.0840722197085 }, "southwest" : { "lat" : 37.42156911970850, "lng" : -122.0867701802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" }'; $json = json_decode($string); $postal = FALSE; foreach( $json->results[0]->address_components as $component ) { if( in_array('postal_code', $component->types) ) $postal = $component->long_name; } if( !$postal ) echo "Postal couldn't be found"; else echo "Postal is $postal"; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362605 Share on other sites More sharing options...
jbradley04 Posted July 19, 2012 Author Share Posted July 19, 2012 Thank you for your help! I am an amateur, at best, when it comes to PHP. I am trying to learn but I havent found any great courses that really help me with arrays and objects. If you know of any, I am more than happy to learn! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/265532-need-help-extracting-just-zip-code-from-this-data/#findComment-1362803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.