Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone

Ok now we have everything setup to start learning Linq in PHP using YaLinqo.  First thing to understand is how to use any data array with YaLinqo so that we can perform different operations on the dataset. We simply cannot work on PHP arrays, we need to convert that to YaLinqo Enumerable object first.

How to Initialize

Let’s go through the important parts of the above code, as you saw in previous post first few lines are just to include the YaLinqo libraries. In Line 7: we simply use the YaLinqo Enumerable class with its namespace so that we don’t have to write entire namespace+class when we need to use it. Then in Line 9: We define an array with integers and nothing new about this as its simply PHP way of defining an array. However it next line we pass it to Enumerable::from() function so we get YaLinqo Enumerable object that can be used to perform different kind of Linq operations that we’re going to discuss throughout this tutorial series.

Output Enumerable  to Arrays and Lists

Now we have Enumerable class so how do we get array object back from this. There are couple of methods but here we’re going to learn two major functions - toArray() and toList(). You may not realize the difference of these two at first, but once you started using key indexed arrays you will the difference.

Hm… what’s the difference, both toArray() and toList() are same?. Well yes in this instance it is same, but I will show the difference in next section where you have key => value based arrays.

I think you got the difference, nothing changed from previous example except this time we have key => value based array as out sample data set and notice that toArray() produced the exact same array we provided. But toList() only produced the list of items (values) eliminates the keys (simply it eliminates the index set).

Simple Aggregate Functions

Finally will see few aggregate functions that can be performed on given data collection using Linq. As you can now understand the usage of YaLinqo I will not be explaning each and every function given below, there will be a comment against each line to explain what each function does. Here we use sum(), max(), min(), count(), average(), first(), last(), distinct()  functions to perform different mathematical operations and to access elements. This is the most basic form of usage of these functions and we will discuss these again in future posts.

 


Posts of this series
How to use Linq in PHP - Part 01
How to use Linq in PHP - Part 02 - Setup
How to use Linq in PHP - Part 03 - Basic Operations
How to use Linq in PHP - Part 04 - Basic Operations
How to use Linq in PHP – Part 05 – Sorting

 

 

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone