PHP code example of mezon / functional

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

    

mezon / functional example snippets


$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

// will display array( 1 , 2 ,3 )
var_dump( \Mezon\Functional\Fetcher::getFields( $Data , 'foo' ) );

$Values = array( 1 , 2 , 3 );
$obj1 = new stdClass();
$obj2 = new stdClass();

$Data = array( $obj1 , $obj2 );

Functional::setFieldsInObjects( $Data , 'foo' , $Values );
// will display 3 objects
var_dump( $Data );

$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

// will display value 6
var_dump( Functional::sumFields( $Data , 'foo' ) );

$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , array( $obj2 , $obj3 ) );

// will display value 6
var_dump(Functional::sumFields( $Data , 'foo' ));

// will display value 3
var_dump(Functional::sumFields( [
    ['foo'=>1],
    ['foo'=>2]
] , 'foo' ));

/**
*   Transformation function multiplies 'foo' field.
*/
function  transform2x( $Object )
{
    $Object->foo *= 2;

    return( $Object );
}
$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

Functional::transform( $Data , 'transform2x' );
// will display 3 objects
// with 2, 4 and 6 values in their 'foo' fields
var_dump( $Data );

$data = [
	1 , 2
];

Transform::convert($data,function($item){return [10*$item, 100*$item];});

var_dump($data);

// will output
// [10=>100 , 20=>200]


$data = [
	1 , 2
];

Transform::convertElements($data,function($item){return 10 * $item;});

var_dump($data);

// will output
// [0=>10 , 1=>20]