From ca9a5d50186b22c0248d5a46874b9ed8a964207e Mon Sep 17 00:00:00 2001 From: lar-dragon Date: Fri, 18 Aug 2017 11:38:57 +0500 Subject: [PATCH] Expression rule for current node and fix getting unique nodes --- src/Peekmo/JsonPath/JsonPath.php | 3 +++ src/Peekmo/JsonPath/JsonStore.php | 17 +++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Peekmo/JsonPath/JsonPath.php b/src/Peekmo/JsonPath/JsonPath.php index 40f4d67..8a8cf8a 100644 --- a/src/Peekmo/JsonPath/JsonPath.php +++ b/src/Peekmo/JsonPath/JsonPath.php @@ -164,6 +164,9 @@ private function trace($expr, $val, $path) $this->trace($this->evalx($loc, $val, substr($path, strrpos($path, ";") + 1)) . ";" . $x, $val, $path); } else if (preg_match("/^\?\(.*?\)$/", $loc)) { // [?(expr)] + if ($this->evalx(preg_replace("/^\?\((.*?)\)$/", "$1", $loc), $val)) { + $this->trace($x, $val, $path); + } $this->walk($loc, $x, $val, $path, array(&$this, "_callback_05")); } else if (preg_match("/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/", $loc)) { // [start:end:step] phyton slice syntax diff --git a/src/Peekmo/JsonPath/JsonStore.php b/src/Peekmo/JsonPath/JsonStore.php index 2c5b4f4..e3e6114 100644 --- a/src/Peekmo/JsonPath/JsonStore.php +++ b/src/Peekmo/JsonPath/JsonStore.php @@ -106,16 +106,13 @@ public function get($expr, $unique = false) if (true === $unique) { if (!empty($values) && is_array($values[0])) { - array_walk($values, function(&$value) { - $value = json_encode($value); - }); - - $values = array_unique($values); - array_walk($values, function(&$value) { - $value = json_decode($value, true); - }); - - return array_values($values); + $tmpValues = array_map(function($value) { + return json_encode($value); + }, $values); + + $tmpValues = array_unique($tmpValues); + + return array_values(array_intersect_key($values, $tmpValues)); } return array_unique($values);