开发者

Automatically filling out array parameters?

Okay, here's my current code for an array.

    $sites[0]['url'] = "http://example0.com;
    $sites[1]['url'] = "http://example1.com;
    $sites[2]['url'] = "http://example2.com;
    $sites[3]['url'] = "http://example3.com;
    $sites[4]['url'] = "http://example4.com;

What I want to do is perhaps make a php page with something like

$sites_to_put_in_array =
    http://example0.com
    http://example1.com
 开发者_开发百科   http://example2.com

And so forth, so that to add a new site to the array, I only need to add the url. Not really a necessary addition, just something I think will make it easier in the future for expanding my script.

And ideas on how to set that up?


Using the $array[] syntax, rather than $array[NUMBER], forces PHP to append the new item to the end of your array, so your numbered array index becomes redundant.

$sites[] = array('url' => "http://example0.com");
$sites[] = array('url' => "http://example1.com");
$sites[] = array('url' => "http://example2.com");
$sites[] = array('url' => "http://example3.com");
$sites[] = array('url' => "http://example4.com");

It's more than just adding the URL, but it's a tradeoff between processing overhead and convenience.


Something like:

$sites_to_put_in_array =
   'http://example0.com
    http://example1.com
    http://example2.com';

$arr = preg_split('/\s+/',$sites_to_put_in_array);
$sites = array();
foreach($arr as $site) {
    $sites[]['url'] = $site;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜