Created
July 11, 2013 07:12
-
-
Save sheeep/5973211 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo | |
{ | |
public $data = array(); | |
public function addToOffset() | |
{ | |
$args = func_get_args(); | |
$value = $args[0]; | |
array_shift($args); | |
$element =& $this->data; | |
foreach ($args as $offset) { | |
if (isset($element[$offset]) && is_array($element[$offset])) { | |
$element =& $element[$offset]; | |
} else { | |
throw new \InvalidArgumentException("The specified path does not exsist or is not an array at " . $offset); | |
} | |
} | |
$element = $value; | |
} | |
} | |
$a = new Foo(); | |
$a->data = array( | |
'files' => array( | |
array( | |
'id' => 1 | |
), | |
array( | |
'id' => 2 | |
) | |
) | |
); | |
$a->addToOffset(array('error' => 'foo'), 'files'); | |
var_dump($a->data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⎈ ⇢ php offset.php | |
array(1) { | |
'files' => | |
array(1) { | |
'error' => | |
string(3) "foo" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment