note 52138 added to function.array
| From: | brian at blueeye dot us | Date: | Fri, 22 Apr 2005 00:34:58 +0000 |
| Subject: | note 52138 added to function.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88378@lists.php.net to get a copy of this message | ||
If you need, for some reason, to create variable Multi-Dimensional Arrays, here's a quick
function that will allow you to have any number of sub elements without knowing how many elements
there will be ahead of time. Note that this will overwrite an existing array value of the same path.
<?php
// set_element(array path, mixed value)
function set_element(&$path, $data) {
return ($key = array_pop($path)) ? set_element($path, array($key=>$data)) : $data;
}
?>
For example:
<?php
echo "<pre>";
$path = array('base', 'category', 'subcategory', 'item');
$array = set_element($path, 'item_value');
print_r($array);
echo "</pre>";
?>
Will display:
Array
(
[base] => Array
(
[category] => Array
(
[subcategory] => Array
(
[item] => item_value
)
)
)
)
----
Manual Page -- http://www.php.net/manual/en/function.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52138
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52138&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52138&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52138&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52138&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52138&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52138&report=yes
Search -- http://master.php.net/manage/user-notes.php