PHP code example of penobit / linq

1. Go to this page and download the library: Download penobit/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/ */

    

penobit / linq example snippets


use Penobit\Linq\Linq;
$linq = new Linq($data);

$linq->collect(['id'=>1, 'name'=>'Penobit']);

linq($data);

use Penobit\JsonQ\Linq;

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 2)
    ->get('name', 'price');

// or you could use $q->select('name', 'price') or $q->fetch('name', 'price')  instead of getch

var_dump($res->toArray());

//This will print
/*
array:3 [▼
  1 => {#7 ▼
    +"name": "macbook pro"
    +"price": 150000
  }
  4 => {#8 ▼
    +"name": "macbook air"
    +"price": 110000
  }
  5 => {#9 ▼
    +"name": "macbook air 1"
    +"price": 81000
  }
]
*/

$result = $linq->from('products')
        ->where('cat', '=', 2)
        ->sum('price');

var_dump($result);

//It will print:
/*
365000
*/

$q = new Linq($data);
echo $q->find('vendor.name');

$q = new Linq($data);
echo $q->find('vendor.name');

$q = new Linq($data);
echo $q->from('vendor.name')->get();

$q = new Linq($data);
echo $q->from('users.5.visits')->get();

$q = new Linq($data);
$res = $q->from('users')->where('id', '=', 1)->get();
// Or
$res = $q->from('users')->where('id', 1)->get();

$q = new Linq($data);
$res = $q->from('users')
    ->where('id', '=', 1)
    ->where('location', '=', 'Tehran')
    ->get();

$q = new Linq($data);
$res = $q->from('users')
    ->callableWhere(function($item) {
        return $item['price'] > 3000;
    })
    ->toArray();

$res = $q->from('users')
    ->where('id', 1)
    ->callableWhere(function($item) {
        return $item['location'] == 'Tehran' &&  substr($item['name'], 0, 3) == 'mac';
    })
    ->orCallableWhere(function($item) {
        return $item['price'] > 3000;
    })
    ->toArray();

$q = new Linq($data);
$res = $q->from('users')
    ->where('id', '=', 1)
    ->orWhere('id', '=', 2)
    ->get();
// Or
$res = $q->from('users')
    ->where('id', 1)
    ->orWhere('id', 2)
    ->get();

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->sum('price');

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->count();

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->max('price);

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->min('price');

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->avg('price');

$q = new linq('data.json');
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->first();

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->last();

$q = new Linq($data);
$res = $q->from('products')
  ->where('cat', '=', 1)
  ->nth(2);

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->exists();

$q = new Linq($data);
$res = $q->from('users')
    ->groupBy('location')
    ->get();

$q = new Linq();
$res = $q->collect([7, 5, 9, 1, 3])
    ->sort();

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->sortBy('price', 'desc');