Need help creating 3D array.
Here's the story. Right now, I have a page that pulls data into two 2D arrays from our database based on a date input.
Code:
$sql="SELECT * FROM database WHERE date='".mysql_real_escape_string($date)."' ORDER BY name;";
$result=mysql_query($sql,$link);
while($row=mysql_fetch_row($result)){
$result_array[]=$row;
if(!in_array($row[1],$index_array)){
$index_array[]=$row[1];
}
}
I then have two loops farther down that cycles through the ENTIRE $result_array comparing the current result entry against the index entry. This isn't that bad when there's only a few hundred entries, but when I try to run it for several thousand entries, things start to bog down. What I'd like to do is create a 3D array, using the index as the leading entry.
Like this:
$result_array[index][row][column]
But I don't want to duplicate the index entry. How could I check if the index entry is already in the array and, if so, it puts that $row into the corresponding spot. (I really wish I could word that better.)
Sort of a:
if $row[1] is in the $result_array[index], add $row to $result_array[that_index][] else create a new index entry and add $row to $result_array[new_index][].
Let me know if I could make my request any more confusing, I'll see what I can do.
bryceowen, January 29th, 2009 08:52 PM
Bookmarks