PHP code example of jwadhams / json-logic-php

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

    

jwadhams / json-logic-php example snippets


json_decode('{"==" : ["apples", "apples"]}', true);
// ["==" => ["apples", "apples"]]

$rule = '{"==":["apples", "apples"]}';

//Decode the JSON string to an array, and evaluate it.
JWadhams\JsonLogic::apply( json_decode($rule, true) );
// true

//Decode the JSON string to an object, and evaluate it.
JWadhams\JsonLogic::apply( json_decode($rule, false) );
// true

JWadhams\JsonLogic::apply( [ "==" => [1, 1] ] );
// true

JWadhams\JsonLogic::apply(
	[ "and" => [
		[ ">" => [3,1] ],
		[ "<" => [1,3] ]
	] ]
);
// true

( (3 > 1) and (1 < 3) )

JWadhams\JsonLogic::apply(
	[ "var" => ["a"] ], // Rule
	[ "a" => 1, "b" => 2 ]   // Data
);
// 1

JWadhams\JsonLogic::apply(
	[ "var" => "a" ],
	[ "a" => 1, "b" => 2 ]
);
// 1

JWadhams\JsonLogic::apply(
	[ "var" => 1 ],
	[ "apple", "banana", "carrot" ]
);
// "banana"

$rules = [ "and" => [
	[ "<" => [ [ "var" => "temp" ], 110 ] ],
	[ "==" => [ [ "var" => "pie.filling" ], "apple" ] ]
] ];

$data = [ "temp" => 100, "pie" => [ "filling" => "apple" ] ];

JWadhams\JsonLogic::apply($rules, $data);
// true

//Always
JWadhams\JsonLogic::apply(true, $data_will_be_ignored);
// true

//Never
JWadhams\JsonLogic::apply(false, $i_wasnt_even_supposed_to_be_here);
// false
bash
curl -O https://raw.githubusercontent.com/jwadhams/json-logic-php/master/src/JWadhams/JsonLogic.php