Welcome to WebmasterWorld Guest from 54.163.157.48

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

dynamic multidimensional arrays

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

Full Member

10+ Year Member

joined:July 5, 2004
posts:303
votes: 0


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)

New User

10+ Year Member

joined:Sept 19, 2003
posts:22
votes: 0


$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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members