开发者

Easiest Way to Change All Array's Keys?

I'm looking for way to change all array's keys to one key for a开发者_如何学Goll elements.

array(4) (
  "a" => string(4) "foo1"
  "b" => string(4) "foo2"
  "c" => string(4) "foo3"
  "d" => string(4) "foo4"
)

...to:

array(4) (
  "a" => string(4) "foo1"
  "a" => string(4) "foo2"
  "a" => string(4) "foo3"
  "a" => string(4) "foo4"
)

I would prefer code without any loops.


array(4) (
  "a" => string(4) "foo1"
  "a" => string(4) "foo2"
  "a" => string(4) "foo3"
  "a" => string(4) "foo4"
)

This is not possible in PHP. Array can not have more than one key with the same name.

But you can do as

$array2['a'] = array_values($array);


As said by Pekka in comment:

You can't have two identical keys in the same array. How would you tell them apart?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜