Page 1 of 1

Filtering an array

Posted: Tue Apr 22, 2008 5:09 pm
by Parody
I've come to a brick wall in my script that I'm writing and I wondered why I couldn't find a php function that simply did this:

(This is the build up to the function):
Grab an array of data from a mysql database, it is multi-dimensional as the first 'layer' contains each row and each row then contains the data for the row in an array.

Then (this is what the function should do):
Search the array of data from the mysql table for a string (this could occur in the second layer so this should also be searched) and then return only the rows of the array which contain the string in array format.

The closest I could find was array_filter(), but it requires a function for each filter which I can't do because I'm carrying out hundreds of different filters on the same original array. I can't use mysql to filter the results (using a new query for each filter) as this would be extremely innefficient.

If someone has written a function which does this or knows of one that exists could they please help me out?

I'd be really grateful.

Thanks

Re: Filtering an array

Posted: Tue Apr 22, 2008 5:17 pm
by Christopher
Parody wrote:but it requires a function for each filter which I can't do because I'm carrying out hundreds of different filters on the same original array.
But above you said that you just wanted to search for matches? Which is it?
Parody wrote:I can't use mysql to filter the results (using a new query for each filter) as this would be extremely innefficient.
Why? It is almost always better to let the database do this kind of work...

Re: Filtering an array

Posted: Wed Apr 23, 2008 9:29 am
by Parody
I am making a calendar of a whole year which displays the events for each day. I am getting all the events for the year in one query and then into an array and then as the while loop reaches cycles through all 365 days it drops the events into each one. Would it be better to run a filter 365 times to find the events in the array for that day or run 365 queries, one for every day as it is reached?

That is the only way I can see of using mysql to filter the events.

If you know how this could be achieved efficiently please tell me :D

Re: Filtering an array

Posted: Wed Apr 23, 2008 11:21 am
by Christopher
I would "ORDER BY date" in the query and use that fact to simplify the code -- you would know a that all records for a day are contiguous. You could loop 1-365 and keep a second counter for the position in the results array.