Project

General

Profile

1
<?php
2
function geolocate($regionName) {
3
	//echo "\nwill geolocate for ".$regionName."\n"; 
4
	//return array('long_name' => $regionName,'lat' => 0,'lon' => 0);
5
	
6
	$r = new HttpRequest('//maps.googleapis.com/maps/api/geocode/json', HttpRequest::METH_GET);
7
	$r->addQueryData(array('address' => urlencode($regionName), 'sensor' => 'false'));
8

    
9
	$r->send();
10

    
11
	if ($r->getResponseCode() == 200) {
12
		$resp = json_decode($r->getResponseBody());
13

    
14
		if (count($resp->results) == 0)
15
			return null;
16
		else {
17
			return array(
18
				'long_name' => $resp->results[0]->address_components[0]->long_name,
19
				'lat' => $resp->results[0]->geometry->location->lat,
20
				'lon' => $resp->results[0]->geometry->location->lng);
21
		}
22

    
23
	} else {
24
		return null;
25
	}
26
}
27
//returns array with all members, or subarrays in array that match the key and value
28
function search($array, $key, $value){
29
    $results = array();
30

    
31
    if (is_array($array))
32
    {
33
        if (isset($array[$key]) && $array[$key] == $value)
34
            $results[] = $array;
35

    
36
        foreach ($array as $subarray)
37
            $results = array_merge($results, search($subarray, $key, $value));
38
    }
39

    
40
    return $results;
41
}
42

    
43
function in_array_r($needle, $haystack) {
44
    $found = false;
45
    foreach ($haystack as $item) {
46
    if ($item === $needle) { 
47
            $found = true; 
48
            break; 
49
        } elseif (is_array($item)) {
50
            $found = in_array_r($needle, $item); 
51
            if($found) { 
52
                break; 
53
            } 
54
        }    
55
    }
56
    return $found;
57
}
58

    
59
function unique($array, $key){
60
	$results = array();
61
	
62
	if(is_array($array)) {
63
		
64
		foreach($array as $elem)
65
			if(isset($elem[$key]) && !in_array($elem[$key],$results))
66
				array_push($results,$elem[$key]);	
67
	}
68
	
69
	return $results;
70
}
71

    
72
function toxml($array,$outerkey,$tabs) {
73
	$xml = "";
74
	foreach($array as $key=>$value) {
75
		if(is_array($value)) {
76
			if(is_numeric($key)) {
77
				$xml .= gt($tabs)."<".htmlspecialchars($outerkey).">".toxml($value,$outerkey,$tabs+1).gt($tabs)."</".htmlspecialchars($outerkey).">";	
78
			}
79
			else {
80
				if(isAssoc($value)) {
81
					$xml .= gt($tabs)."<".htmlspecialchars($key).">".toxml($value,$key,$tabs+1).gt($tabs)."</".htmlspecialchars($key).">";
82
				}				
83
				else {
84
					$xml .= toxml($value,$key,$tabs);
85
				}			
86
			}
87
		}
88
		else {
89
			$xml .= gt($tabs)."<".htmlspecialchars($key).">".htmlspecialchars($value)."</".htmlspecialchars($key).">";
90
		}
91
	
92
	}
93

    
94
	return $xml;
95
}
96

    
97
function isAssoc($arr) {
98
    return array_keys($arr) !== range(0, count($arr) - 1);
99
}
100

    
101
function gt($num) {
102
	$t = "\n";
103
	while($num--) 
104
		$t .= "\t";
105
	return $t;
106
}
107

    
108
function monthName($monthNum) {
109

    
110
	switch($monthNum) {
111
		case 1: return 'January';
112
		case 2: return 'February';
113
		case 3: return 'March';
114
		case 4: return 'April';
115
		case 5: return 'May';
116
		case 6: return 'June';
117
		case 7: return 'July';
118
		case 8: return 'August';
119
		case 9: return 'September';
120
		case 10: return 'October';
121
		case 11: return 'November';
122
		case 12: return 'December';
123
	}
124

    
125
}
126

    
127
?>
(10-10/28)