如何从SQL查询更改密钥名称

发布于 2025-01-24 03:31:26 字数 794 浏览 1 评论 0原文

到目前为止,我一直在寻找或一种更改对象密钥名称的方法。我需要能够通过结果集中的键对所有键进行解析,检查键的名称,并为其分配一个新的密钥名称(而非值),如果密钥的名称与变量$名称匹配。我将在下面包含我的代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2025-01-31 03:31:26

有一个简单的解决方案来更新任何对象的密钥。您必须使用新的和未设置的旧物体存储对象的价值。

// Start
while($row = mysqli_fetch_assoc($result)) {

    $row->new_Key = $row->old_key;
    unset($row->old_key);
    array_push($infocusArray, $row);
};

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.

// Start
while($row = mysqli_fetch_assoc($result)) {

    $row->new_Key = $row->old_key;
    unset($row->old_key);
    array_push($infocusArray, $row);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文