Forum Moderators: coopster

Message Too Old, No Replies

Checking to see if key exists in multi-dimensional array

         

techtheatre

3:54 am on Dec 21, 2009 (gmt 0)

10+ Year Member



Arrays always end up making my head hurt...but even more so when it becomes a multidimensional array. I am running several queries against a MySQL database, and in some situations i may get the same result a couple times...so i am putting my results into an array, and only will add the new result if it is not already stored in the array. I figure that this is a good way to make sure i end up with all my info, and no duplicates...and then i can do stuff with my data. Here is what i have so far (and feel free to suggest another way to handle the array if there is another approach i am missing):

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!

ALKateb

11:42 am on Dec 21, 2009 (gmt 0)

10+ Year Member



I'm not sure if the experts here would suggest a better way but i once had this issue and i solved it by building 1D array that only contains the IDs of the found records then i use in_array on that array to check whether this record has been found before or not

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

7:17 pm on Dec 21, 2009 (gmt 0)



Why don't you just check to see if it exists using a MySQL query instead of storing everything into memory in an array? This is the approach I would take. Either way you have to make a query, whether you are grabbing the data or checking to see if it already exists in the table. The latter, however, will use less resources and be easier to implement.

techtheatre

8:19 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



ALKateb: I also had tried your approach with the ID being the index, but kept hitting a wall. I will try again with this approach and post my results (or failures).

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?

techtheatre

7:07 am on Dec 22, 2009 (gmt 0)

10+ Year Member



problem solved.

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.