[PHP] Creating multidimensional array

[PHP] Creating multidimensional array

Post by joche » Fri, 22 Dec 2006 22:10:31


you can do this with a bit of reference magic...

<?php

$ref = null;
$keys = array("six","five","four","three","two","one");
$val = "string value"; // the node value
$arr = array(); // generated array

$ref =& $arr;

while ($key = array_pop($keys)) {
$ref[$key] = array();
$ref =& $ref[$key];
}

$ref = $val;
var_dump($arr);

?>

there might a cleaner/better way - if anyone can correct me I'd
be glad to learn :-)
 
 
 

[PHP] Creating multidimensional array

Post by rober » Fri, 22 Dec 2006 22:19:17


<?php

$new = array();
$ref = &$new;
foreach( array_reverse( $array ) as $key )
{
$ref[$key] = array();
$ref = &$ref[$key];
}

$ref['node_title'] = 'string value';

print_r( $new );

?>

Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.yqcomputer.com/ |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'