Skip to content

Commit

Permalink
Added O::assocPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 5, 2021
1 parent 4b76392 commit 4fd8b20
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/O.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4fd8b20

Please sign in to comment.