PHP/MYSQL替换字典数组中键的文本
我有一个MYSQL查询,它获取通过api中的JSON返回的字典或结果数组。在某些情况下,我希望更改结果数组中字典键的名称 例如,在以下情况下:PHP/MYSQL替换字典数组中键的文本,php,mysql,arrays,dictionary,str-replace,Php,Mysql,Arrays,Dictionary,Str Replace,我有一个MYSQL查询,它获取通过api中的JSON返回的字典或结果数组。在某些情况下,我希望更改结果数组中字典键的名称 例如,在以下情况下: $var = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]'; 我想把它改为: $var = '[{"golfer":"Tiger Woods"},{"golfer":"Gary Player"}]' 在这种情况下,更改mysql查询是不实际的,因此我只想将关键字的单词“player”替换为
$var = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]';
我想把它改为:
$var = '[{"golfer":"Tiger Woods"},{"golfer":"Gary Player"}]'
在这种情况下,更改mysql查询是不实际的,因此我只想将关键字的单词“player”替换为单词“golfer”,而不干扰值
在上面的示例中,我不想更改Gary Player的名称,因此仅使用str_replace似乎不起作用
有没有一种方法可以在不更改任何值的情况下将所有键从“player”更改为“golfer”?您可以将该键的值写入新键,然后删除旧键 将名为“a”的键重命名为“b”,同时保留该值
var json = {
"a" : "one"
}
json["b"] = json["a"];
delete json["a"];
对于您的示例,只需将其与循环一起使用
来源:这是您可以使用的代码片段
$var = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]';
// json decode the json string
$temp = json_decode($var, true);
$temp = array_map(function($item){
// combining key and values
return array_combine(['golfer'], $item);
}, $temp);
print_r($temp);
// or echo json_encode($temp);
有人认为foreach是最快的
foreach($temp as $k => &$v){
$v = array_combine(['golfer'], $v);
}
print_r($temp);
如果单个数组中有多个键,则几乎没有硬编码
foreach ($temp as $k => &$v){
$v['golfer'] = $v['player'];
unset($v['player']);
}
print_r($temp);
使用递归
function custom($arr, $newKey, $oldKey)
{
// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($arr)) {
return $arr;
}
$result = []; // empty array to hold copy of subject
foreach ($arr as $key => $value) {
// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;
// add the value with the recursive call
$result[$key] = custom($value, $newKey, $oldKey);
}
return $result;
}
$var = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]';
$temp = json_decode($var, true);
$temp = replaceKey($temp, 'golfer', 'player');
print_r($temp);
&
使用json方式
function json_change_key($arr, $oldkey, $newkey) {
$json = str_replace('"'.$oldkey.'":', '"'.$newkey.'":', json_encode($arr));
return json_decode($json, true);
}
$temp = json_change_key($temp, 'player', 'golfer');
print_r($temp);
如果您想要多个密钥替换,这里有一个技巧
$var = '[{"player":"Tiger Woods", "wins":"10","losses":"3"},{"player":"Gary Player","wins":"7", "losses":6}]';
$temp = json_decode($var, true);
function recursive_change_key($arr, $set)
{
if (is_array($arr) && is_array($set)) {
$newArr = [];
foreach ($arr as $k => $v) {
$key = array_key_exists($k, $set) ? $set[$k] : $k;
$newArr[$key] = is_array($v) ? recursive_change_key($v, $set) : $v;
}
return $newArr;
}
return $arr;
}
$set = [
"player" => "golfers",
"wins" => "victories",
"losses" => "defeats"
];
$temp = recursive_change_key($temp, $set);
print_r($temp);
$a = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]';
$array = json_decode($a, true);
foreach($array as $key=>$value){
if(array_keys($value)[0] === "player"){
$array[$key] = ["golfer" => array_values($value)[0]];
};
}
echo json_encode($array);