note 52419 deleted from ref.array by tularis
| From: | tularis@php.net | Date: | Sun, 26 Aug 2007 16:40:27 +0000 |
| Subject: | note 52419 deleted from ref.array by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129817@lists.php.net to get a copy of this message | ||
Note Submitter: casey geene
----
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');
?>