tags:

views:

55

answers:

2

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?

A: 

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;
}
codaddict
A: 

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.

Adam Backstrom
So even if I put $sites[1] as $sites[5], as long as it was still in that same place, it would still be $sites[1]?Correct me if I misunderstood.
Rob
If you leave out the number, it would append. I've updated the answer with the full syntax you could use.
Adam Backstrom