logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 07-11-2012, 10:00 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default multidimensional array from a variable

I am trying a few exercises and my latest one is to remove the websites extensions, so I'm just left with the sites name then make a multidimensional array from those values.Then using a loop, add new elements with keys and values.

My problem is:
How do put those values into an array?

I have made this for part one of the excercise

$sites = array();
$sites[] = "http://www.facebook.com/";
$sites[] = "http://www.argos.co.uk/";
$sites[] = "http://www.next.co.uk/";
$sites[] = "http://www.halfords.com/";
$sites[] = "https://twitter.com/";
$sites[] = "http://www.comet.co.uk/";

$remove = array("http://www.", "https://", ".co.uk/", ".com/");

foreach($sites AS $k => $v){
$taken = str_replace($remove, "", $v);
echo ucfirst($taken) . "<br/>";
}
**outputs**

Facebook
Argos
Next
Halfords
Twitter
Comet

**outputs**

I have tried putting another foreach loop inside the one I have, I have also tried creating an empty array before running the first loop, im guessing im close to the answer but i cant work it out at the moment.
tony09uk is offline   Reply With Quote
Old 07-11-2012, 10:11 PM   #2
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

Ive managed to place the values into an array

$sites = array();
$sites[] = "http://www.facebook.com/";
$sites[] = "http://www.argos.co.uk/";
$sites[] = "http://www.next.co.uk/";
$sites[] = "http://www.halfords.com/";
$sites[] = "https://twitter.com/";
$sites[] = "http://www.comet.co.uk/";

$remove = array("http://www.", "https://", ".co.uk/", ".com/");

$newarray = array();
foreach($sites AS $k => $v){
$taken = str_replace($remove, "", $v);
echo ucfirst($taken) . "<br/>";
array_unshift($newarray, ucfirst($taken));
}
print_r($newarray);

Now I will get started on the next part

is there a better way to do this or is this an acceptable way?
tony09uk is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com