From 4fd8b2052bcd873c570eceb8a562606c89425824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lajos=20M=C3=A9sz=C3=A1ros?= Date: Thu, 5 Aug 2021 16:51:17 +0200 Subject: [PATCH] Added O::assocPath() --- src/O.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/O.php b/src/O.php index 3b84536..f8251d2 100644 --- a/src/O.php +++ b/src/O.php @@ -58,6 +58,19 @@ public static function assoc(string $key, $value, $data) return $data; } + // O::assocPath(['foo', 'bar'], 12, {}) -> {foo: {bar: 12}} + function assocPath($path, $value, $data) { + $tmp = &$data; + + for ($i = 0; $i < count($path); $i++) { + $tmp = &$tmp[$path[$i]]; + } + + $tmp = $value; + + return $data; + } + // O::dissoc('foo', {foo: 'bar', fizz: 'buzz'}) -> {fizz: 'buzz'} public static function dissoc(string $key, $data) {