CLiown
CLiown

Reputation: 13843

Creating a simple array

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?

Upvotes: 3

Views: 152

Answers (3)

deceze
deceze

Reputation: 522024

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

Upvotes: 6

codaddict
codaddict

Reputation: 454970

You can do:

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

Upvotes: 2

Ben Everard
Ben Everard

Reputation: 13804

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

Upvotes: 0

Related Questions