Forum Moderators: coopster
while(condition){
//do some stuff
//create an array $dirarray[]
//amend values in array
//save as new array each time? e.g.$dirarray1[], $dirarray2[] etc. until loop finished
}
$x = 0;
while(condition){
$x++;
${'dirarray'.$x} = $data;
}
$i=0;
while(condition){
$arrName = "dirarray".$i;
$$arrName[0] = "something";
$$arrName[1] = "something";
$$arrName[2] = "something";
$$arrName[3] = "something";
$i++;
}
for($j=0; $j<$i; $j++){
$arrName = "dirarray".$j;
print_r($$arrName);
}
But then tested and saw that something wierd was heppening when trying to use variable variables as array names.. (it was like $i was getting overwritten with then value of the variable).. Does anyone know why this is?.
Anyway you could use a 2d array to store you values
$i=0;
while(condition){
$dirArray[$i][0] = "something";
$dirArray[$i][1] = "something";
$dirArray[$i][2] = "something";
$dirArray[$i][3] = "something";
$i++;
}print_r($dirArray);
Ally
$i=0;
while($i<3){
$dirArray[$i][0] = "something";
$dirArray[$i][1] = "something";
$dirArray[$i][2] = "something";
$dirArray[$i][3] = "something";
$i++;
}
print_r($dirArray);
?>
Gives the following result:
Array
(
[0] => Array
(
[0] => something
[1] => something
[2] => something
[3] => something
)
[1] => Array
(
[0] => something
[1] => something
[2] => something
[3] => something
)
[2] => Array
(
[0] => something
[1] => something
[2] => something
[3] => something
)
)
Shouldn't this be the result? Where do you think is the confusion?
Habtom
$$arrName[0]is trying to reference the array element "0" of the $arrName variable to get a string to use as a variable name. In other words, it's identical to writing it:
${$arrName[0]};
To get it to use $arrName as the array name, then also reference a particular index of the new array, you just need to use curly braces in the right places:
${$arrName}[0];
I had a bit of trouble with this, I think due to the fact that my surrounding code was getting a bit complicated and I didn't want to post all of it! In the end I went for
//save as new array each time
$mddirarray[] = $dirarray;
unset($dirarray);
This is good, but what I really want to do with this multidimensional array is loop through it and display it as nested HTML lists. Anyone know how to do this?
my surrounding code was getting a bit complicated and I didn't want to post all of it!
I've been wondering what your [webmasterworld.com] recent [webmasterworld.com] threads [webmasterworld.com] combine to produce. ;)
It might be a good idea to start another thread and post a bigger chunk of code at this point. A holistic view might give an opportunity to troubleshoot the root cause of these other complications. Up to you of course. :)