2

I'm wondering if there is a way to filter out an array like you can do in SQL queries. Like WHERE, OR, LIMIT, ORDER BY (etc). I'm caching my tables in JSON files to avoid unnecessary mysql connections.

Let me explain

This is my example array

    $array = array(
    0 => array('value1', 'value2', 'value3', 'value4', 'value5', 'value6', 'value7'),
    1 => array(1, '-1', 1, 'Google.com', 'http://google.com/', 'about:blank', 1),
    2 => array(2, '-1', 2, 'Yahoo.com', 'http://yahoo.com/', 'about:blank', 1),
    3 => array(1, '-1', 1, 'Bing.com', 'http://bing.com/', 'about:blank', 3),
    4 => array(1, '-1', 1, 'Youtube.com', 'http://youtube.com/', 'about:blank', 3),
    5 => array(1, '-1', 1, 'Facebook.com', 'http://facebook.com/', 'about:blank', 4),
    6 => array(1, '-1', 1, 'Stackoverflow.com', 'http://stackoverflow.com/', 'about:blank', 3),
);

Now I wanna filter my array out.

Lets say I just want records where value7 is 3 or value3 is Bing.com, or value2 is -1. Is this possible todo without hundreds of loops and checks?

Is it worth it or will a SQL server cost less?

1
  • 2
    Check out: phplinq.codeplex.com, I believe this is what you are looking for Commented Jun 4, 2013 at 19:22

3 Answers 3

1

You might be able to write your own filters if you only have a couple. Otherwise, SQL might be a better way to store/query this data:

http://php.net/manual/en/function.array-filter.php

Sign up to request clarification or add additional context in comments.

Comments

0

You could use a flavour of Linq for PHP. Linq is originally from ASP.net, and it's use is to filter datasets using a language similar to SQL. A quick google gave me this: http://phplinq.codeplex.com/ , but it seems it has not been updated since 2009. Perhaps there are other implementations around.

3 Comments

Yes. I'm looking at Lind right now, it hasn't been updated since 2009. I'm gonna let this topic stay open and see if anything else pops up!
But something like Lind is indeed what I am looking for.
It could be fine. AFAIK Linq is not that new, and if the implementation is complete in this case, there would be little reason to continue development.
0

This is better suited for a database.

You might be able to get around having to get a SQL server by using a file system database like SQLite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.