Forum Moderators: coopster
each loop in the where clause (going through results from my MySQL query) has the following line:
$MapResults[] = array( SiteId => "$id", SiteName => "$name", Miles => "$distance" );
This is making an array where the thee values I am getting from my database (id, name, distance) are being stored in an array. I want to be able to say something like this:
if( in_array($id,$MapResults) )
{
echo "$SiteName is already in array!";
}
else
{
//add the new record
$MapResults[] = array( SiteId => "$id", SiteName => "$name", Miles => "$distance" );
}
My troubles seem to lie with the "in_array" function...i cannot figure out how to "in_array" the sub-array(s) rather than the "shell" array. I have tried every permutation of it that I can think of...but cannot seem to get it working. THANKS!
but i think you do it a better way.
like when you are forming your 2D array make the index of each element the ID of the found record then when you find new record what you could do is to check whether $MapResults[$id] isset or not if not then you add it like this $MapResults[$id] (notice here the index of the 2D array elements is the record ID itself)
eelixduppy: I don't want to clutter my database with a temporary table just for this one purpose. I cannot do the final insert into the real table until i have all my records compiled because i do some additional processing of the data before the insertion that relies on the complete resultset in order to be computed.
Everyone: Is there really not a way to check to see if a value is in the inner part of a multi-dimensional array? This seems like a fairly reasonable function that would come up often...am i wrong?
I changed my array to the following so that the SiteId is now the key:
$MapResults["$id"] = array( SiteName => "$name", Distance => "$distance" );
Then I use the following to determine if I have already stored this site in my array:
if( array_key_exists("$id", $MapResults) )
{
echo "<strong>$SiteName is already in array!</strong><br />";
}else{
//do stuff...
}
thanks.