tags:

views:

70

answers:

3
$items  = "word1 word2 word3 word4";
$items = explode(" ", $items);

$items is my array, how can I have it turn into this

Like how can I make word1 be a key and a value?

"word1" => "word1"
+8  A: 

$newArray = array_combine($items, $items);

Tom Haigh
That seems like the best way, it didn't work how I hoped though \, my previous code posted above, "word1" actually is canada territories and some of them have a space in them, British Columbia Manitoba New Brunswick, that is 3 locations but my function made it into 5 values in the array, how can I get around this?
jasondavis
why the downvote?
Tom Haigh
I think you'll struggle to parse that string into an array, because it requires knowledge of the places. Could you get the source string with a different separator?
Tom Haigh
You really need a better separator, something that is never used in the values you want to retrieve. Perhaps a semicolon?
pcguru
+1  A: 
foreach($items as $item) {
$newArr[$item] = $item;
}
Charles Ma
I think your foreach arguments are the wrong way around
Tom Haigh
have corrected, hope that's ok
Tom Haigh
+1  A: 
$items  = "word1 word2 word3 word4";
$temp = explode(" ", $items);
$result = array();
foreach ($temp as $value) {
  $result[$value] = $value; 
}
Alex