tags:

views:

77

answers:

3

Yes I'm a n00b!

Now that's out of the way, I have the following code:

$page=3;

$i=1;

    while($i<=$pages) {
      $urls .= "'"."http://twitter.com/favorites.xml?page=" . $i ."',";
      $i++;
    }

What I need to create is this array:

$data = array('http://twitter.com/favorites.xml?page=1','http://twitter.com/favorites.xml?page=2','http://twitter.com/favorites.xml?page=3');

How can I produce an array from the while loop?

+6  A: 
$urls = array();
for ($x = 1; $x <= 3; $x++) {
    $urls[] = "http://twitter.com/favorites.xml?page=$x";
}

. is for concatenating strings.
[] is for accessing arrays.
[] = pushes a value onto the end of an array (automatically creates a new element in the array and assigns to it).

deceze
+1, however it would be better to retain the $pages variable and use that in the for loop condition
Jonathan Fingland
Ain't it supposed to assign an index to the array $urls[$x] = "http://twitter.com/favorites.xml?page=$x";
JeremySpouken
Quicker and simpler, +1 :)
ILMV
@Jeremy no, danits example doesn't suggest this, $url[] will auto assign a key.
ILMV
@Jeremy, the index is not required, the $urls[] syntax is equivalent to array_push($urls, ....)
Jonathan Fingland
If the `$page` value is needed any further it's easy enough to rewrite the loop accordingly, I'll leave that as an exercise to the OP. :)
deceze
I think you need $x inside the string , not $i ?
Tom Haigh
@Tom Oops, good catch. Copied that line… :)
deceze
A: 

Try this instead:

$page=3;

$i=1;
$url=array();

while($i<=$pages) {
    $urls[]="http://twitter.com/favorites.xml?page=".$i ;
    $i++;
}

echo("<pre>".print_r($url,true)."</pre>");
ILMV
+2  A: 

You can do:

$page=3;
$i=1;    
$data = array();
while($i <= $page) {
    $data[] = "http://twitter.com/favorites.xml?page=" . $i++;
}
codaddict
Your url value isn't correct, lose the quotes at each end and the comma, he just wants the URL.
ILMV
@ILMV: Thanks man :)
codaddict