homepage Welcome to WebmasterWorld Guest from 54.237.95.6
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
dynamic multidimensional arrays
supermanjnk




msg:1306784
 4:17 pm on Apr 18, 2006 (gmt 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)

 

Finger




msg:1306785
 9:28 pm on Apr 20, 2006 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved