Alternative to json_encode
Posted: Sat May 08, 2010 2:24 am
I'm using json_encode to pass some database data to my google maps generator. The script takes about 200+ entries, gets their latitude, longitude, and name, and then sends that to javascript to make markers on the map. The script takes about .4 seconds to parse through each entry and push its info into the $results array, but takes a whopping 20+ seconds to run json_encode on that finished array.
Is there another way of outputting a json-encoded array? Or am I doing it wrong?
Here's the script:
Is there another way of outputting a json-encoded array? Or am I doing it wrong?
Here's the script:
Code: Select all
private function set_json_info() {
$script_start = microtime_float();
$city = get_table_data('cities', 'name', $this->display_metro);
$area = get_table_data('regions', 'name', $this->display_area);
$sub_area = get_table_data('sub_areas', 'name', $this->display_sub_area);
if($this->display_sub_area) {
$this->map_title = "$city - $area - $sub_area";
$this->query_listings("object_class=venue&table=venues&area=".$this->display_area."&sub_area=".$this->display_sub_area);
}
elseif($this->display_area) {
$this->map_title = "$city - $area";
$this->query_listings("object_class=venue&table=venues&area=".$this->display_area);
}
else {
$this->map_title = $city;
$this->query_listings("object_class=venue&table=venues&metro=".$this->display_metro);
}
while($this->has_results()) {
unset($result);
$nf_listing = $this->create_object();
$result['lat'] = $nf_listing->venue_latitude;
$result['lng'] = $nf_listing->venue_longitude;
$result['name'] = '<a href="'.$nf_listing->venue_url().'">'.$nf_listing->venue_name.'</a>';
$address1 = $nf_listing->venue_address1;
$address2 = $nf_listing->venue_address2;
$city = $nf_listing->venue_city;
$state = $nf_listing->venue_state;
$zip = $nf_listing->venue_zip;
if($address2) {
$address2 = $address2."<br />";
}
$address = $address1."<br />".$address2.$city.", ".$state." ".$zip;
$result['address'] = $address;
$return[] = $result;
}
$script_end = microtime_float();
$time = $script_end - $script_start;
echo "Script took $time seconds to run."; // .4 seconds
$this->map_json_info = json_encode($return);
$script_end = microtime_float();
$time = $script_end - $script_start;
echo "Script took $time seconds to run."; // 20+ seconds!!!
}