3

Consider following scenario: You have a small MogoDB collection or a small MySQL table (contains 1000 record for example, it may be read-only too ) that is used extensively and repeatedly in your app.

You decide to fetch all of its content to a PHP array and use the array instead of sending a lot of query to DB.

In above scenario how can we retrieve some array items in a manner similar to retrieving records of a DB table in an efficient way? A PDO adapter for array or any other similar facility ...

Handsome Nerd
  • 15,997
  • 20
  • 92
  • 166

2 Answers2

2

There is port of famous C# LINQ library for php.

It gives you flexible oo interface to querying data sources.

http://phplinq.codeplex.com/wikipage?title=Examples&referringTitle=Home

Simple example

// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric"); 

$result = from('$name')->in($names)
            ->where('$name => strlen($name) < 5')
            ->select('$name'); 
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Fivell
  • 11,514
  • 3
  • 58
  • 95
1

While you could use a combination of array_filter and the function niehztog proposed at php.net ( http://pl1.php.net/manual/en/function.array-filter.php#87912 ), I believe even with small amounts of data you will still find querying the db faster than trying to achieve the same with array processing.

Remember that MySQL caches query results - so you just hit memory with repeated queries and not the disk.

Pawel J. Wal
  • 1,136
  • 1
  • 12
  • 23