PHP修改键值:
/**
* replace array key
* @param $array
* @param $oldKey
* @param $newKey
* @return array
* @throws \Exception
* @internal param $old_key
* @internal param $new_key
*/
public function replace_key($array, $oldKey, $newKey) {
$keys = array_keys($array);
if (false === $index = array_search($oldKey, $keys)) {
throw new \Exception(sprintf('Key "%s" does not exit', $oldKey));
}
$keys[$index] = $newKey;
return array_combine($keys, array_values($array));
}
直接调用这个方法即可
$arr = array(
'vien' => 'vvv',
'data' => 'aaa'
);
replace_key($arr, 'vien', 'tech')
输出结果
['tech' => 'vvv',
'data' => 'aaa']
viencoding.com版权所有,允许转载,但转载请注明出处和原文链接: https://viencoding.com/article/118
White - 5 years ago
It worked for me, thx.
Vien - 5 years ago
You're welcome. ✧(≖ ◡ ≖✿)