在PHP中从循环添加到数组,但不添加更多键

在PHP中从循环添加到数组,但不添加更多键,php,Php,当从循环中添加项时,我不确定如何使生成的数组/对象看起来像我想要的,但试图阻止为每个项创建新的键 我需要最终输出看起来像这样,没有单独的数字键 ['item-1' => 'blue widget', 'item-2' => 'red widget', 'item-3' => 'white widget'] 还是这个 Array ( [item-1] => blue widget [item-2] => red widget [item-3] =>

当从循环中添加项时,我不确定如何使生成的数组/对象看起来像我想要的,但试图阻止为每个项创建新的键

我需要最终输出看起来像这样,没有单独的数字键

['item-1' => 'blue widget', 'item-2' => 'red widget', 'item-3' => 'white widget']
还是这个

Array (
  [item-1] => blue widget
  [item-2] => red widget
  [item-3] => white widget
)
下面给出了示例代码:

$items = array('blue widget', 'red widget', 'white widget');
$final = array();
$count_items = 1;
foreach ($items as $item) {
  $item_num = 'item-'.$count_items;
  $count_items++;
  $final[] = [$item_num => $item];
}
print_r($final);
但这给了我这样一个输出:

Array ( 
  [0] => Array ( [item-1] => blue widget ) 
  [1] => Array ( [item-2] => red widget ) 
  [2] => Array ( [item-3] => white widget ) 
)
我也尝试了array_push,但得到了相同的结果


谢谢

有时候我们会有荷马的时刻。。。DOH

总之,正如@billyonecan所指出的,我需要这样做:

$items = array('blue widget', 'red widget', 'white widget');
$final = array();
$count_items = 1;
foreach ($items as $item) {
  $item_num = 'item-'.$count_items;
  $count_items++;
  $final[$item_num] = $item; //FIXED THIS LINE
}
print_r($final);
这将提供适当的输出:

Array ( 
  [item-1] => blue widget 
  [item-2] => red widget 
  [item-3] => white widget 
)

$final[$item\u num]=$itemDoh!!当然thanks@jsherk因为和就像一个印刷错误,你们可以删除它。我在下面贴了答案,我认为这个问题应该保留,因为这是一个与编程相关的问题,也可能是其他人也有过的问题。