用“for”循环填充PHP数组

问题描述:

I'm trying to populate an array in PHP as following :

<?php

$maxPages = 20;

for ($i = 0; $i <= $maxPages; $i++) {

    $url = 'http://127.0.0.1/?page='.$i;

    $targets =  array(
            $url => array(
                    CURLOPT_TIMEOUT => 10
            ),
    );

}

print_r($targets);

?>

However it only seems to display the last populated value:

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

)

I also tried changing : " $targets = " to " $targets[] = " however it produces this 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 my desired output is :

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 an easy fix but I'm unable to see it. Can someone with more knowledge point me out my mistake ?

Thanks !

我正在尝试在PHP中填充数组,如下所示: p>

  &lt;?php 
 
 $ maxPages = 20; 
 
for($ i = 0; $ i&lt; = $ maxPages; $ i ++){
 
 $ url ='http://  127.0.0.1/?page='.$i;

 $ targets = array(
 $ url =&gt; array(
 CURLOPT_TIMEOUT =&gt; 10 
),
); 
 
}  
 
print_r($ targets); 
 
?&gt; 
  code>  pre> 
 
 

但是它似乎只显示最后填充的值: p> 数组 ( [http://127.0.0.1/?page=20] =&gt;数组 ( [13] =&gt; 10 ) \ n) code> pre>

我也尝试过更改:“$ targets =”到“$ targets [] =”但它产生了这个输出: p> \ n

  [0] =&gt; 数组
(
 [http://127.0.0.1/?page=0] =&gt;数组
(
 [13] =&gt; 10 
)
 
)
 
 [1  ] =&gt; 数组
(
 [http://127.0.0.1/?page=1] =&gt;数组
(
 [13] =&gt; 10 
)
 
)
 
 [2  ] =&gt; 数组
(
 [http://127.0.0.1/?page=2] =&gt;数组
(
 [13] =&gt; 10 
)
 
)
  code>   pre> 
 
 

虽然我想要的输出是: p>

  Array 
(
 [http://127.0.0.1/?page=0  ] =&gt;数组
(
 [13] =&gt; 10 
)
 
 [http://127.0.0.1/?page=1] =&gt;数组
(
 [13]  =&gt; 10 
)
 
 [http://127.0.0.1/?page=2] =&gt;数组
(
 [13] =&gt; 10 
)
  code>   pre> 
 
 

) p>

可能是一个简单的修复但我无法看到它。 如果有更多知识的人指出我的错误? p>

谢谢! p> div>

Try this Code :

$maxPages = 20;
$targets = array();
for ($i = 0; $i <= $maxPages; $i++) {

    $url = 'http://127.0.0.1/?page='.$i;

        $targets[$url] =  array(
            CURLOPT_TIMEOUT => 10
        );

}
echo "<pre>";
print_r($targets);

As simple as changing the loop body to:

$targets[] =  array( // <-- NOTE THE []
        $url => array(
                CURLOPT_TIMEOUT => 10
        ),
);

$targets[] = array(
        $url => array(
                CURLOPT_TIMEOUT => 10
        ),
);

Use [] to append the array to $targets instead of overwriting.

$targets = array();
for ($i = 0; $i <= $maxPages; $i++) {

  $url = 'http://127.0.0.1/?page='.$i;

  **$targets[]** =  array(
        $url => array(
                CURLOPT_TIMEOUT => 10
        ),
  );

}

use targets[] to create a new array each loop

So from what we deduced in the comments: Your first issue is that you're reassigning the $targets variable in the loop rather than appending to the array using the [] notation.

Then we discovered that you need to set the key of $targets to be $url so that's easily fixed by adding $url into the square brackets [$url]. Which gives us:

$targets[$url] = array(
    CURLOPT_TIMEOUT => 10
);