Hi all, I'm decoding a JSON object into an array and then inserting it into a database, which informs markers on a google map. That step is working fine, but I'm now trying to insert code to (a) read whether the latitude value in the array is null, (b) send the address info to be geocoded via the Google Geocoding API if so, and then (c) insert the resulting latitude and longitude values back into the array, to be inserted into the database.

I'm new to PHP and am quite the rookie. I'm getting the same result from the below as I was getting before I included the If statement. Something I'm doing with my function or the array values isn't right, and it's probably pretty elementary. Could anyone offer advice?

// define event1 array
	$event1 = array(
	'id' => $json->id,
	'owner_name' => $json->owner->name,
	'owner_cat' => $json->owner->category,
	'owner_id' => $json->owner->id,
	'event_name' => $json->name,
	'event_description' => $json->description,
	'genre' => $genre,
	'start_time' => $json->start_time,
	'end_time' => $json->end_time,
	'location' => $json->location,
	'venue_street' => $json->venue->street,
	'venue_city' => $json->venue->city,
	'venue_state' => $json->venue->state,
	'venue_zip' => $json->venue->zip,
	'venue_country' => $json->venue->country,
	'venue_lat' => $json->venue->latitude,
	'venue_long' => $json->venue->longitude,
	'venue_id' => $json->venue->id,
	'privacy' => $json->privacy,
	'updated_time' => $json->updated_time,
	);

// If latitude value above = NULL, geocode

	if ($event1['venue_lat'] = NULL) {
		$location_clean = str_replace(" ", "+", $event1['location']);
		$street_clean = str_replace(" ", "+", $event1['venue_street']);
		$city_clean = str_replace(" ", "+", $event1['venue_city']);
		$state_clean = str_replace(" ", "+", $event1['venue_state']);
		$zip_clean = str_replace(" ", "+", $event1['venue_zip']);
		$country_clean = str_replace(" ", "+", $event1['venue_country']);

		$string = $location_clean . ",+" . $street_clean . ",+" . $city_clean . ",+" . $state_clean . ",+" . $zip_clean . ",+" . $country_clean;	

		$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $string . "&sensor=false";
			
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $details_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$geoloc = json_decode(curl_exec($ch), true);

// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
	   if ($geoloc['status'] != 'OK') {
		return null;
	   }

	$event1['venue_lat'] = ($geoloc['results'][0]['geometry']['location']['lat']);
	$event1['venue_long'] = ($geoloc['results'][0]['geometry']['location']['lng']); 
  
	print_r($event1['venue_lat']);
	print_r($event1['venue_long']);
}	

// implode event1 array to $event2
	$event2 = '"'.implode(array(
	$event1['id'],
	$event1['owner_name'],
	$event1['owner_cat'],
	$event1['owner_id'],
	$event1['event_name'],
	$event1['event_description'],
	$event1['genre'],
	$event1['start_time'],
	$event1['end_time'],
	$event1['location'],
	$event1['venue_street'],
	$event1['venue_city'],
	$event1['venue_state'],
	$event1['venue_zip'],
	$event1['venue_country'],
	$event1['venue_lat'],
	$event1['venue_long'],
	$event1['venue_id'],
	$event1['privacy'],
	$event1['updated_time']
	),'","').'"';

basicly you are setting the null there not testing for it

if ($event1['venue_lat'] = NULL) {

should be 

if ($event1['venue_lat'] == NULL) {

in php
= sets variable
== looks to see if variables are same case doesnt matter
=== looks to see if variables are exactly the same

commented: Great help, thanks Hitman! +0

Hah, perfect, that's all it was. Thanks so much Hitman - I probably could have spent a day trying to work that out!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.