如何构建这个php数组?

时间:2009-07-30 21:29:43

标签: php arrays

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

$ items是我的数组,我怎么能把它变成这个

我怎样才能让word1成为一把钥匙和一个价值?

"word1" => "word1"

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:1)

foreach($items as $item) {
$newArr[$item] = $item;
}

答案 2 :(得分:1)

$items  = "word1 word2 word3 word4";
$temp = explode(" ", $items);
$result = array();
foreach ($temp as $value) {
  $result[$value] = $value; 
}