Skip to content

Instantly share code, notes, and snippets.

@sheeep
Created July 11, 2013 07:12
Show Gist options
  • Save sheeep/5973211 to your computer and use it in GitHub Desktop.
Save sheeep/5973211 to your computer and use it in GitHub Desktop.
<?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);
⎈ ⇢ 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