PHPackages                             jwadhams/json-logic-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jwadhams/json-logic-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jwadhams/json-logic-php
=======================

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

1.5.1(1y ago)1641.7M—9.9%24[5 issues](https://github.com/jwadhams/json-logic-php/issues)[7 PRs](https://github.com/jwadhams/json-logic-php/pulls)6MITPHPPHP &gt;=7.2.0

Since Oct 15Pushed 1y ago7 watchersCompare

[ Source](https://github.com/jwadhams/json-logic-php)[ Packagist](https://packagist.org/packages/jwadhams/json-logic-php)[ RSS](/packages/jwadhams-json-logic-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (31)Used By (6)

json-logic-php
==============

[](#json-logic-php)

This parser accepts [JsonLogic](http://jsonlogic.com) rules and executes them in PHP.

The JsonLogic format is designed to allow you to share rules (logic) between front-end and back-end code (regardless of language difference), even to store logic along with a record in a database. JsonLogic is documented extensively at [JsonLogic.com](http://jsonlogic.com), including examples of every [supported operation](http://jsonlogic.com/operations.html) and a place to [try out rules in your browser](http://jsonlogic.com/play.html).

The same format can also be executed in JavaScript by the library [json-logic-js](https://github.com/jwadhams/json-logic-js/)

Examples
--------

[](#examples)

### A note about types

[](#a-note-about-types)

This is a PHP interpreter of a format designed to be transmitted and stored as JSON. So it makes sense to conceptualize the rules in JSON.

Expressed in JSON, a JsonLogic rule is always one key, with an array of values.

```
{"==" : ["apples", "apples"]}
```

PHP has a way to express associative arrays as literals, and no object equivalent, so all these examples are written as if JsonLogic rules were decoded with [`json_decode`'s `$assoc` parameter set true](http://php.net/manual/en/function.json-decode.php), e.g.

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

The library will happily accept either associative arrays or objects:

```
$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
```

### Simple

[](#simple)

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

This is a simple test, equivalent to `1 == 1`. A few things about the format:

1. The operator is always in the "key" position. There is only one key per JsonLogic rule.
2. The values are typically an array.
3. Each value can be a string, number, boolean, array, or null

### Compound

[](#compound)

Here we're beginning to nest rules.

```
JWadhams\JsonLogic::apply(
	[ "and" => [
		[ ">" => [3,1] ],
		[ "
