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

This part of PHP Linq tutorial will also going over more basic operations that YaLinqo has to offer. Understanding these primary simple operations first will enable you to understand what Linq is even if you’re new and it will also help you to understand and apply more complex Linq operations in later posts. Here we will check more output options available in YaLinqo and their usage.

More Output Options

In last part we studied about toList() and toArray() operations and we also discussed the main difference of them. Here I will be explaining toKeys(), toJSON(), toString() and toValues() operations.


This will simply return array of key values of given associated array. If your data collection is like this [ 'data01' => 'value01', 'data02' => 'value02' ] then this operation will return array similar to ['data01', 'data02']. You may find native PHP functions will also support these without much effort, But I’m just trying to explain available features with YaLinqo so that you can use them in single line of code without switching to native way of doing certain things. Let’s see working example of it.


I don’t think I need to explain it as it is obvious by its name. Instead of returning keys this will return values as an array.


This is also another function that you may be able to perform using PHP json_encode(). But if you’re performing filtering joining and projecting complex collection and then need to get JSON output in same line, using YaLinqo built in toJSON() function is much easier and very readable. You may not realize the benefit right now but when you go through more advance Linq operations later in this series it will be very evident. For now just trust my word :). If your data collection like in previous example; [ 'data01' => 'value01', 'data02' => 'value02' ] and if you perform toJSON() on that you will get result similar to {"data01" : "value01", "data02" : "value02"}. In following example I have used both YaLinqo toJSON() and PHP json_encode() function to show you the similarity.


This is very useful function that whenever you have collection of data and you need simply to print them separated by a character (ex: comma separated list) then this is the function to use. For ex: if you have array [10, 20, 30, 40] and you wish to print it in human readable format such as “10, 20, 30, 40″ you can use YaLinqo toString() directly by giving the character of your choice to separate elements. In following example simply check that how we use toKeys() we learned just few minutes ago. Also note that we can even combine the elements without a separator as in last example below.

I will go bit more advance here and show you some cool things you can do with YaLinqo. YaLinqo by default consider two string literals very specific and you can use those two to instruct YaLinqo to act upon then accordingly. Those two are $k and $v These two has special meaning $k represent the key in associated array and $v represents the value. You can use these two to tell YaLinqo what you how to use them in certain logic combinations. For different functions these two literals can be used in different ways. In above example we use toString() in two different occasions to print keys and values. But using $k and $v you can do it single operation. toString() function takes second argument where you can specify how you want the output entry is formatted before they are concatenated with separator string. Look at following example.

Here we use formatting string "[$k] = $v" to tell toString function show key and value in this given format. If you look at the output below you will understand what I meant.

Isn’t this powerful and easy to use? I think it really is.

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