Re: Creating multidimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 :-)

Yonatan Ben-Nes wrote:
> Hi all,
> 
> I got a problem with creating a multidimensional array which it's size is
> unknown.
> I already made it work like this (example):
> <?php
> $array = array('six','five','four','three','two','one');
> 
> for ($i = count($array)-1; $i >= 0; $i--) {
>  $array_keys_string_representation .= '["'.$array[$i].'"]';
> }
> 
> eval('$new_array'.$array_keys_string_representation.'["node_title"] =
> "string value";');
> ?>
> 
> The problem is that I don't like using eval() cause it's slow &
> insecure.....
> 
> I want to do something like this (doesn't work):
> <?php
> $array = array('six','five','four','three','two','one');
> 
> for ($i = count($array)-1; $i >= 0; $i--) {
>  $array_keys_string_representation .= '["'.$array[$i].'"]';
> }
> 
> $new_array.$array_keys_string_representation.'["node_title"] = "string
> value";
> ?>
> 
> Anyone got any idea how can I build such an array without eval()?
> 
> Thanks a lot in advance,
>  Ben-Nes Yonatan
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux