2010-03-31 154 views
3

我有以下代码:创建一个简单的数组

$page=3; 

$i=1; 

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

我需要创建的这个数组:

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

我怎么能生产出从while循环数组?

+2

*(参考)* http://de2.php.net/manual/en/language.types.array。 php – Gordon 2010-03-31 12:18:04

回答

0

试试这个:

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

.用于连接字符串。
[]用于访问数组。
[] =将一个值推到数组的末尾(自动在数组中创建一个新元素并分配给它)。

+0

+1,但是最好保留$ pages变量并在for循环条件中使用 – 2010-03-31 12:20:49

+0

是不是应该为数组赋值一个索引$ url [$ x] =“http:// twitter .COM/favorites.xml页= $ X?“; – 2010-03-31 12:20:57

+0

更快,更简单,+1 :) – 2010-03-31 12:21:10

2

你可以这样做:

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

您的网址值不正确,在每一端丢失引号和逗号,他只是想要网址。 – 2010-03-31 12:22:44

+0

@ILMV:谢谢你:) – codaddict 2010-03-31 12:24:16