3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class StringPathAccess { protected $data = array(); public function __construct(array $data) { $this->data = $data; } public function &get($key) { if (strpos($key, '[]') !== false) { $key = str_replace('[]', '', $key); } $parts = explode('[', $key); $value = &$this->data; foreach ($parts as $part) { $part = rtrim($part, ']'); if (isset($value[$part])) { $value = &$value[$part]; } else { return null; } } return $value; } public function set($key, $value) { $val = &$this->get($key); $val = $value; } } $data = array( 'foo' => array( 'bar' => 'baz' )); $object = new StringPathAccess($data); $test = $object->get('foo[bar]'); var_dump($object->get('foo[bar]')); $object->set('foo[bar]', 'Hello World'); var_dump($object->get('foo[bar]'));
Output for 5.1.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
string(3) "baz" string(11) "Hello World"
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/IotST on line 6
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IotST on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IotST on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/IotST on line 4
Process exited with code 255.