如何从SQL查询更改密钥名称
到目前为止,我一直在寻找或一种更改对象密钥名称的方法。我需要能够通过结果集中的键对所有键进行解析,检查键的名称,并为其分配一个新的密钥名称(而非值),如果密钥的名称与变量$名称匹配。我将在下面包含我的代码:
$infocusArray = array();
if($result = $conn->query($query))
// Start
while($row = mysqli_fetch_assoc($result)) {
array_push($infocusArray, $row);
};
foreach($row as $key => $element){
echo $key; => outputs key names
};
file_put_contents('data.json', json_encode($infocusArray));
echo count($infocusArray);
}
data.json的json_encode($ infocusArray)的结果:
[
{
"id": "ai-101010",
"sector": "guidance",
"date": "10-11-2030"
},
{
"id": "ai-191011",
"sector": "airfield",
"date": "03-01-2023"
}
]
我正在使用的数据集更大得多,但是出于简单起见,我正在删除大多数项目。
I have been looking or a way to change an objects key name with no success as of yet. I need to be able to parse ALL through the keys in the resulting set, check the name of the key, and assign it a new key name (NOT VALUE) if the name of the key matches a variable $name. I will include my code below:
$infocusArray = array();
if($result = $conn->query($query))
// Start
while($row = mysqli_fetch_assoc($result)) {
array_push($infocusArray, $row);
};
foreach($row as $key => $element){
echo $key; => outputs key names
};
file_put_contents('data.json', json_encode($infocusArray));
echo count($infocusArray);
}
data.json result of json_encode($infocusArray):
[
{
"id": "ai-101010",
"sector": "guidance",
"date": "10-11-2030"
},
{
"id": "ai-191011",
"sector": "airfield",
"date": "03-01-2023"
}
]
The dataset I am working with is much larger however I am removing most items for simplicity sake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个简单的解决方案来更新任何对象的密钥。您必须使用新的和未设置的旧物体存储对象的价值。
There is a simple solution for updating key of any object. You have to store the value of object with new and unset old one.