PHP code example of erdalceylan / array-join

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

    

erdalceylan / array-join example snippets


$users = [
    ["id"=>1, "nick"=>"erdal"],
     (object)["id"=>2, "nick"=>"furkan" ],
    ["id"=>3, "nick"=>"huseyin"],
    ["id"=>4, "nick"=>"hümeyra" ],
    ["id"=>5, "nick"=>"tuba" ],
];

 $items = [
     ["user_id"=>1, "item"=>"kaban", "mmx" => "mmx1"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx2"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx3"],
     (object)["user_id"=>1, "item"=>"çorap", "mmx" => "mmx4"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx5"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx6"],
    ["user_id"=>2, "item"=>"araba", "mmx" => "mmx7"],
     (object)["user_id"=>9, "item"=>"ev", "mmx" => "mmx8"],
    ["user_id"=>10, "item"=>"yat", "mmx" => "mmx9"],
];

$foods = [
    ["user_id"=>1, "food"=>"iskender"],
    ["user_id"=>2, "food"=>"adana"],
];

$texts = [
    ["user_id"=>1, "text"=>"merhaba"],
    ["user_id"=>15, "text"=>" hi"],
];

 $instance = \ArrayJoin\Builder::newInstance()
     ->select("a.nick")
     ->from($users, "a")
     ->setFetchType(\ArrayJoin\Builder::FETCH_TYPE_ARRAY);
     
 $instance->execute();
 
 //output
 array (
   array ('nick' => 'erdal'),
   array ('nick' => 'furkan'),
   array ('nick' => 'huseyin'),
   array ('nick' => 'hümeyra'), 
   array ('nick' => 'tuba',)
);

 $instance = \ArrayJoin\Builder::newInstance()
     ->select("a.id", "a.nick", "b.item")
     ->from($users, "a")
     ->innerJoin($items, "b", new \ArrayJoin\On("a.id = b.user_id"))
     ->setFetchType(\ArrayJoin\Builder::FETCH_TYPE_ARRAY)
     ->offset(1)
     ->limit(2);
     
 $instance->execute();
 //output
 array (
   array ('id' => 1,'nick' => 'erdal','item' => 'çorap',),
   array ('id' => 1,'nick' => 'erdal','item' => 'çorap',)
 );

$instance = \ArrayJoin\Builder::newInstance()
    ->select("a.id", "a.nick", "b.item", "d.food")
    ->from($users, "a")
    ->innerJoin($items, "b", new \ArrayJoin\On("a.id = b.user_id"))
    ->leftJoin($texts, "c", new \ArrayJoin\On("a.id = c.user_id"))
    ->rightJoin($foods, "d", new \ArrayJoin\On("b.user_id = d.user_id"))
     ->where(function ($id, $text, $item, $food){
         return $id < 50;
     }, "a.id", "c.text", "b.item", "d.food")
     ->groupBy("a.id", "d.food")
     ->limit(2)
     ->offset(1)
     ->setFetchType(\ArrayJoin\Builder::FETCH_TYPE_OBJECT);
     
 $instance->execute();
 
 array (
   stdClass::__set_state(array(
      'id' => 1,
      'nick' => 'erdal',
      'item' => 'çorap',
      'food' => 'iskender',
   )),
   stdClass::__set_state(array(
      'id' => 1,
      'nick' => 'erdal',
      'item' => 'çorap',
      'food' => 'iskender',
   )),
 );