I have a multidimensional array from a a MySQL database call. It looks something like this:
array(2) {
[0]=> array(4) {
["STATE"]=> "1"
["COUNTY"]=> "101"
["NEIGHBORHOOD"]=> "111111"
}
[1]=> array(4) {
["STATE"]=> "2"
["COUNTY"]=> "201"
["NEIGHBORHOOD"]=> "222222"
}
[2]=> array(4) {
["STATE"]=> "1"
["COUNTY"]=> "201"
["NEIGHBORHOOD"]=> "111111"
}
}
I need to be able to get the neighborhood values for every county in every state that appears in the array. The data structure is that the neighborhood number is unique to the county, which is unique to the state.
I am thinking about creating an array for each county in each state, but I am not sure how to go about finding all the the values of neighborhood when state and county are identical.
Per the request in the comments, I have a DB with the information for the STATE, COUNTY, NEIGHBORHOOD, LATITUDE, LONGITUDE. I am using the following SQL Statement to get all NEIGHBORHOODS within a user selected radius of a user inputed location.
"SELECT `STATE` , `COUNTY` , `NEIGHBORHOOD`,((ACOS(SIN($lat* PI() / 180) * SIN(`LATITUDE` * PI() / 180) +
COS($lat* PI() / 180) * COS(`LATITUDE` * PI() / 180) * COS(($long- `LONGITUDE`) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
FROM `censustracts`
HAVING distance<='$radius';"
These NEIGHBORHOODS can be in different STATES and COUNTIES, and I have no way of knowing this.
Perhaps there’s a better way to do this by not getting duplicates through your SQL query, but you could hash your unique values to ensure duplicates are removed.
That should leave you with an array like this:
With this structure in mind, you an encapsulate this into a class to return things like all the neighborhoods for a given state or county.
At least, the concept is there. The idea is that hashing it like this will remove duplicates as they just end up overwriting each other and it’s nested in a hierarchy, which is sort of how it naturally occurs.
EDIT
I quickly threw a test class together and I updated my code with that class. Mind you, there’s no validity checking, etc. But it could be a start for you to get what you’re looking for.
I hope that helps!