PHP code example of linq / php-linq

1. Go to this page and download the library: Download linq/php-linq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

linq / php-linq example snippets


composer 

protected $students = array(
    array("name" => "Milan", "surname" => "Gallas", "age" => 20, "Job" => "php Programátor"),
    array("name" => "Amdrea", "surname" => "Novotná", "age" => 17, "Job" => "java Programátor"),
    array("name" => "Honza", "surname" => "Pulkert", "age" => 27, "Job" => "c# Programátor"),
    array("name" => "Nikola", "surname" => "Světnická", "age" => 23, "Job" => "php Programátor"),
    array("name" => "Nikola", "surname" => "Světnická", "age" => 23, "Job" => "php Programátor"),
    array("name" => "Petr", "surname" => "Grůdl", "age" => 31, "Job" => "java Programátor"),
);

protected $sports = array(
    array("userName" => "Milan", "sport" => "šachy", "active" => true),
    array("userName" => "Milan", "sport" => "karate", "active" => false),
    array("userName" => "Honza", "sport" => "box", "active" => true),
    array("userName" => "Honza", "sport" => "fotbal", "active" => false),
    array("userName" => "Milan", "sport" => "hokej", "active" => true),
    array("userName" => "Petr", "sport" => "tenis", "active" => true)
);



//init base linq class
$linq = \Linq\LinqFactory::createLinq();

//filtering data - select only users who are older than 23
$linq
	->from($userData)
	->where(function($item){
		if($item["age"] > 23) return $item;
	})

//clasic select
var_dump($linq->select());
//select data with function
var_dump(
		$linq->select(function($person){
			$person["age"]*=2;
			return $person;
		})
	);
//or
var_dump(
		$linq->select(function($user){
			//return $user["name"]  //you can try = return string
			return array("userName" => $user["name"]); //return array
		})
	);

//short select = less typing :D
var_dump( $linq->select("age") );  echo '<br>';
var_dump( $linq->select("name", "age") ); echo '<br>';
//$linq->select("name", "age") =>
var_dump(
	$linq->select(function($user){
		return array($user["name"] => $user["age"]);
	})
);



//full use with join method and groupBy
$linq
	->from($userData)
	//->innerJoin($sportData)
	->leftJoin($sportData)
	->on(function($user, $sport){
		return ($user["name"] == $sport["userName"]);
	})
	->groupBy("Job")
	->select()

$userJsonData = json_encode($userData);
$sportJsonData = json_encode($sportData);

$linq = \Linq\LinqFactory::createJsonLinq();

	echo '<pre>'; var_dump(
		$linq
			->from($userJsonData)
			->takeWhile(function($user){
				return ($user["Job"] === "php Programátor" && $user["age"] > 20);
			})
	);

$sportXml =
'<sports>
     <sport>
         <userName>Milan</userName>
         <sport>šachy</sport>
         <active>true</active>
     </sport>
     <sport>
         <userName>Milan</userName>
         <sport>karate</sport>
         <active>false</active>
     </sport>
         <sport>
         <userName>Milan</userName>
         <sport>hokej</sport>
         <active>true</active>
         </sport>
     <sport>
         <userName>Honza</userName>
         <sport>box</sport>
         <active>true</active>
     </sport>
     <sport>
         <userName>Honza</userName>
         <sport>fotbal</sport>
         <active>false</active>
     </sport>
     <sport>
         <userName>Petr</userName>
         <sport>tenis</sport>
         <active>true</active>
     </sport>
</sports>';

//users
$userXml = '
<students>
     <student>
         <name>Milan</name>
         <surname>Gallas</surname>
         <age>20</age>
         <Job>php Programátor</Job>
     </student>
     <student>
         <name>Amdrea</name>
         <surname>Novotná</surname>
         <age>17</age>
         <Job>Java Programátor</Job>
     </student>
     <student>
         <name>Honza</name>
         <surname>Pulkert</surname>
         <age>27</age>
         <Job>c# Programátor</Job>
     </student>
     <student>
         <name>Nikola</name>
         <surname>Světnická</surname>
         <age>23</age>
         <Job>php Programátor</Job>
     </student>
     <student>
         <name>Nikola</name>
         <surname>Světnická</surname>
         <age>23</age>
         <Job>php Programátor</Job>
     </student>
     <student>
         <name>Petr</name>
         <surname>Grůdl</surname>
         <age>31</age>
         <Job>Java Programátor</Job>
     </student>
</students>
	';

    //first example. Source is string
	echo '<pre>';
	var_dump(
		\Linq\LinqFactory::createXmlLinq()
			->from($userXml)
			->leftJoin($sportXml)
			->on(function($user, $sport){
				return ($user["name"] == $sport["userName"]);
			})
			->select()
	);


	//second example group by and reverse - source is string
	echo "<pre>";
	var_dump(
		\Linq\LinqFactory::createXmlLinq()->from($userXml)->groupBy("Job", "name")->reverse()->select();
	);
	echo "</pre>";



	//third examlple - source is simpleXmlElement. use condition on collection of objects and sort by orderBy function descending
	$xmlElemetn = new SimpleXMLElement($userXml);
	echo '<pre>';
	var_dump(
		\Linq\LinqFactory::createXmlLinq()
			->from($xmlElemetn)
			->where(function($user){
				return (strlen($user->name) > 5);
			})
			->orderBy("age", true)
			->select()
	);