Filling PHP array with "for" loop -


i'm trying populate array in php following :

<?php  $maxpages = 20;  ($i = 0; $i <= $maxpages; $i++) {      $url = 'http://127.0.0.1/?page='.$i;      $targets =  array(             $url => array(                     curlopt_timeout => 10             ),     );  }  print_r($targets);  ?> 

however seems display last populated value:

array ( [http://127.0.0.1/?page=20] => array     (         [13] => 10     )  ) 

i tried changing : " $targets = " " $targets[] = " produces output :

[0] => array     (         [http://127.0.0.1/?page=0] => array             (                 [13] => 10             )      )  [1] => array     (         [http://127.0.0.1/?page=1] => array             (                 [13] => 10             )      )  [2] => array     (         [http://127.0.0.1/?page=2] => array             (                 [13] => 10             )      ) 

while desired output :

array ( [http://127.0.0.1/?page=0] => array     (         [13] => 10     )  [http://127.0.0.1/?page=1] => array     (         [13] => 10     )  [http://127.0.0.1/?page=2] => array     (         [13] => 10     ) 

)

probably easy fix i'm unable see it. can more knowledge point me out mistake ?

thanks !

try code :

$maxpages = 20; $targets = array(); ($i = 0; $i <= $maxpages; $i++) {      $url = 'http://127.0.0.1/?page='.$i;          $targets[$url] =  array(             curlopt_timeout => 10         );  } echo "<pre>"; print_r($targets); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -