Object Subcollection Help

Aug 2, 2010 at 1:25 PM

Here is my example

class Person{

    public  $name;

    public  $items = array();


class Item{

public $itemName;

public $itemId;

public __construcy($itmName, $itmId){

$this->itemName = $itmName;

$this->itemId = $itmId;



$person1 = new Person();

$person2 = new Person();

$person1->items[] = new Item(12,"key 1");

$person1->items[] = new Item(13,"key 2");

$person2->items[] = new Item(12,"key 3");

$person2->items[] = new Item(13,"key 4");

$personArray = array($person1, $person2);

In c#, to get the Person with "key 1", I usually do something like this:


var result = from i in persons
                where i.Items.Exists(delegate(Item t){
                                         return t.ItemName == "key 1";
                select i;

My question now is that how do I do these on php linq? Any kind of help/comment would help..





Aug 3, 2010 at 9:55 AM

Can you try something like this:

$result = from('$i')->in($persons)
          ->where('$i => from(\'$item\')->in($i->items)->any(\'$item => $item->ItemName == "key1"\')')

Aug 23, 2010 at 9:14 AM


This code worked, my only problem now is speed, its a a bit slow when searching inside sub-collections. The site that I'am 

currently working on has 127 products, takes 16 seconds to load a page while finding a specific item... Got an idea how to optimize

the code better?

Thanks, And good job by the way


Sep 17, 2010 at 1:20 PM

I stand corrected, Its fast, I had a bug in my code that it always  fetch from the db instead of fetching from the cache... People should use, phplinq, its so fast in getting 1000 - 5000 rows of in memory data, I tried it... Will post back more when that data goes 10000.


Sep 20, 2010 at 7:59 AM

Interested in these results :-)