<?php
class A
{
private $query;
public function &findNode(array $path)
{
$link = &$this->query;
foreach ($path as $element) {
$link = &$link[$element];
}
return $link;
}
public function setValues(array $path, array $values)
{
$node = &$this->findNode($path);
$node = array_merge($node ?? [], $values);
unset($node);
return $this;
}
public function setValueByPath(array $path, $value): self
{
$link = &$this->findNode($path);
if (is_array($value)) {
$link[] = $value;
} else {
$link = $value;
}
unset($link);
return $this;
}
public function print()
{
var_dump($this->query);
}
}
$a = new A;
$a
->setValues(['c', 'd'], [['test' => 1], ['test' => 2]])
->setValues(['c', 'd'], [['test' => 3], ['test' => 4]]);
$a
->setValueByPath(['a', 'b', 'c', 'd'], ['match' => 100500])
->setValueByPath(['a', 'b', 'c', 'd'], ['match' => 1000])
->setValueByPath(['a', 'b', 'c', 'd', 1, 'zzz'], 'lol');
print_r($a);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- A Object
(
[query:A:private] => Array
(
[c] => Array
(
[d] => Array
(
[0] => Array
(
[test] => 1
)
[1] => Array
(
[test] => 2
)
[2] => Array
(
[test] => 3
)
[3] => Array
(
[test] => 4
)
)
)
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
[0] => Array
(
[match] => 100500
)
[1] => Array
(
[match] => 1000
[zzz] => lol
)
)
)
)
)
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
A Object
(
[query:A:private] => Array
(
[c] => Array
(
[d] => Array
(
[0] => Array
(
[test] => 1
)
[1] => Array
(
[test] => 2
)
[2] => Array
(
[test] => 3
)
[3] => Array
(
[test] => 4
)
)
)
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
[0] => Array
(
[match] => 100500
)
[1] => Array
(
[match] => 1000
[zzz] => lol
)
)
)
)
)
)
)
- Output for 5.6.0 - 5.6.40
- Parse error: syntax error, unexpected '?' in /in/SofHF on line 20
Process exited with code 255.
preferences:
218.6 ms | 401 KiB | 284 Q