Ronald Araújo Ronald Araújo - 1 year ago 111
PHP Question

LINQ expressions?

Is there a way to use LINQ Expressions in PHP?
For example, in C# I can do the following:

List<string> names = new List<string>()

var oneName = names.Where(x => x.Equals("Ronald")).FirstOrDefault();

And in PHP, how would I do something like the following?

names **.Where** (x => x.Equals("Ronald")) **.FirstOrDefault()**;

Answer Source

There are a few PHP libraries that mimic the functionalities of LINQ. Examples are:

In PHPLinq the code would look like this:

$names = array("Francisco", "Ronald", "Araújo", "Barbosa"); 
$oneName = from('$name')->in($names)
            ->where('$x => $x == "Ronald"')

Or with PINQ which takes a different approach with PHP 5.3+ closures:

$oneName = \Pinq\Traversable::from($names)
            ->where(function ($x) { return $x == 'Ronald'; })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download