Forum Moderators: coopster

Message Too Old, No Replies

how to create new array with each loop

hopefully simple question!

         

HelenDev

10:32 am on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a while loop that's doing some stuff, and what I want is for it to create a new array each time. I only seem to be able to add to the existing array each time though - I'm sure there must be a simple way of doing this but it evades me at the moment!


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
}

Habtom

10:36 am on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$x = 0;
while(condition){
$array[$x] = $data;
$x++;
}
?>

whoisgregg

1:19 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use variable variables [php.net] (if you specifically want them to be called $dirarrayx) as shown below. :)

$x = 0;
while(condition){
$x++;
${'dirarray'.$x} = $data;
}

Scally_Ally

1:24 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



I initially started writing this

$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

Habtom

1:29 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php

$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

whoisgregg

1:37 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that
$$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];

HelenDev

1:37 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your posts guys :)

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

Which has resulted in a lovely multidimensional array :)

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?

whoisgregg

1:57 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

HelenDev

2:05 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been wondering what your recent threads combine to produce

LOL! Can you tell I've got a little project? ;)

I'll continue working on it some more to see if I get any further, then I may well post some more code!

Thanks for all your help so far :)

Scally_Ally

2:31 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



I think whoisgreg answered it, thanks all the same though Habtom..

Sorry for almost hijackin your thread helendev.

[edited by: Scally_Ally at 2:33 pm (utc) on June 6, 2007]