From e859bcb38fa99f254bdd839b144a07f11a7794f8 Mon Sep 17 00:00:00 2001 From: Ryan Irelan Date: Sun, 24 Sep 2017 16:58:00 -0500 Subject: [PATCH] Added Assoc array support (#1) * Added support for outputting an assoc array PHP's implementation of json_decode also supports an $assoc param that allows you to output an associative array instead of an object. * some proper formatting * Update README.md --- README.md | 11 ++++++++++- .../twigextensions/JsonDecodeTwigExtension.php | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 14f9273..0d22a8f 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,13 @@ Add extra data to fields like multiselect, in this example add icons from multip {% endfor %} ``` -[Dale Inverarity](http://dale.wtf/) © 2015 - All rights reserved \ No newline at end of file + +## Options + +By default `json_decode` outputs an object but you can also output an associative array by passing `true` as a parameter value to the filter. + +```php +{% set icon = rawicon | json_decode(true) %} +``` + +[Dale Inverarity](http://dale.wtf/) © 2015 - All rights reserved diff --git a/jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php b/jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php index 063f392..707cd9b 100644 --- a/jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php +++ b/jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php @@ -25,8 +25,8 @@ public function getFilters() { return $returnArray; } - public function json_decode($json) { + public function json_decode($json, $assoc = false) { - return json_decode($json); + return json_decode($json, $assoc); } -} \ No newline at end of file +}