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++;
    }

I need to create 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 create an array from a loop while?

+3
source share
3 answers

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>");
0
source
$urls = array();
for ($x = 1; $x <= 3; $x++) {
    $urls[] = "http://twitter.com/favorites.xml?page=$x";
}

.designed to concatenate strings.
[]designed to access arrays.
[] =pushes the value to the end of the array (automatically creates a new element in the array and assigns it).

+6
source

You can do:

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

Source: https://habr.com/ru/post/1739296/


All Articles