views:

32

answers:

1

Currently I have an array that lists email addresses as the key and urls as the values, I want to use parse_url (unless something better is suggested) and create another dimension to the array with the base url. This is what I currently have:

array (
  '[email protected]' =>
  array (
    0 => 'http://www.google.co.uk/foo/bar',
    1 => 'http://www.reddit.com/r/programming/',
  ),
  '[email protected]' =>
  array (
    0 => 'http://stackoverflow.com/faq',
    1 => 'http://de.php.net/parse_url',
  ),
  '[email protected]' =>
  array (
    0 => 'http://www.bleepingcomputer.com/forums/',
  ),
)

And this is what I want the output to be:

array (
  '[email protected]' =>
  array (
    0 => 'http://www.google.co.uk/foo/bar',
      array (
        0=> 'www.google.co.uk'
      )     
    1 => 'http://www.reddit.com/r/programming/',
      array (
        0=> 'www.reddit.com'
      ), 
  ),
  '[email protected]' =>
  array (
    0 => 'http://stackoverflow.com/faq',
      array (
        0=> 'http://stackoverflow.com'  
      )
    1 => 'http://de.php.net/parse_url',
      array (
        0=> 'de.php.net'
      ),
  ),
  '[email protected]' =>
  array (
    0 => 'http://www.bleepingcomputer.com/forums/',
      array (
        0=> 'http://www.bleepingcomputer.com'   
      ),
  ),
)

Any help is very much appreciated!

+1  A: 

I believe this should do the trick:

foreach($some_array as $email => $urllist)
{
    foreach($urllist as $key => $url)
    {
        $some_array[$email][$key] = array($url,parse_url($url,PHP_URL_HOST));
    }
}
Kristoffer S Hansen
Technically doesn't return quite the structure the user requested, but admittedly that's a good thing as the requested structure wasn't valid. You may wish to edit it and add in the missing closing bracket though. :)
Cags