Filtering an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Filtering an array

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Filtering an array

Post 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...
(#10850)
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: Filtering an array

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Filtering an array

Post 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.
(#10850)
Post Reply