PHP - 如何为数组键指定名称而不是使用array_push指定int

2 php

大家好,我有一个从方法返回的数据库结果.我需要再将4个值压入堆栈,但我需要命名密钥.array_push()自动赋值int.我怎样才能克服这种行为?

Array
(
    [these] => df
    [are] => df
    [the] => sdf
    [keys] => sd
    [ineed] => daf
    [0] => something
    [1] => something
    [2] => something
    [3] => something
)
Run Code Online (Sandbox Code Playgroud)

需要更改int值的键.我怎么能用array_push做到这一点?

nic*_*ckf 5

像这样:

$arr['anotherKey'] = "something";
$arr['yetAnotherKey'] = "something";
$arr['andSoOn'] = "something";
Run Code Online (Sandbox Code Playgroud)

要么

$arr = array_merge($arr, array(
    'anotherKey' => "something",
    'yetAnotherKey' => "something",
    'andSoOn' => "something"
));
Run Code Online (Sandbox Code Playgroud)

...但我建议使用第一种方法,因为它只是为数组添加了更多的元素,而第二种方法会有更多的开销(尽管在某些情况下它更灵活).