Welcome to WebmasterWorld Guest from 54.163.100.58

Forum Moderators: coopster & jatar k

dynamic multidimensional arrays

   
4:17 pm on Apr 18, 2006 (gmt 0)

10+ Year Member



I'm trying to get a dynamic multidimensional array that can have unlimited ammounts of keys down based on a database.

$sm_array = array();
while ($fetch = mysql_fetch_object($results)) {
if (count(explode("-", $fetch->parent)) >=2){
$exp = explode("-", $fetch->parent);
$test = "";
foreach ($exp as $key=>$val){
$test .= "[$val]";

$sm_array{$test}[$fetch->id]= "$fetch->title";
}
}

if fetch->parent is something like "test-test-test"
then the key in sm_array ends up being
[[test][test][test]] => Array
()

Is it possible to do what I am trying to do, or does anyone know a better way? (this structure at most will be 5 deep but I don't want to limit it)

9:28 pm on Apr 20, 2006 (gmt 0)

10+ Year Member



$test_str = "test1-test2-test3";

$exp = explode("-",$test_str);

foreach ( $exp as $val ) {
if (!isset($sm_array) ) {
$sm_array[$val] = array();
$last_array = &$sm_array[$val];
} else {
$last_array[$val] = array();
$last_array = &$last_array[$val];
}
}

Using the above $test_str, the code makes the multidimensional array $sm_array[test1][test2][test3]. Is that what you were looking for? Hopefully I understood your question completely.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month