note 52419 added to ref.array
| From: | caseygeene at osu1 dot php dot net | Date: | Sat, 30 Apr 2005 10:14:45 +0000 |
| Subject: | note 52419 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88785@lists.php.net to get a copy of this message | ||
Handy function to walk through an array to get an offset with a given path.
<?php
function &array_path(&$array, $path) {
if(!is_array($array)) {
trigger_error('array_path(): First argument should be an array', E_USER_WARNING);
}
settype($path, 'array');
$offset =& $array;
foreach ($path as $index) {
if (!isset($offset[$index])) {
trigger_error("Undefined offset: $index");
return false;
}
$offset =& $offset[$index];
}
return $offset;
}
$a = array(
1 => array(
2 => array(
3 => 'hi'
)
)
);
echo array_path($a, array(1, 2, 3));
$ref =& array_path($a, array(1, 2));
$ref = array('hello');
?>
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52419
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52419&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52419&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52419&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52419&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52419&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52419&report=yes
Search -- http://master.php.net/manage/user-notes.php