假设我有这个:
$username = (string) $inputs['username'] ?? null;
Run Code Online (Sandbox Code Playgroud)
如果$inputs['username']设置了,那么我希望它转换为string. 如果没有设置,那么$username应该是null。
但是,目前如果$inputs['username']未设置,它将是一个空字符串而不是 null。
我怎样才能解决这个问题?或者这是故意的行为?
假设存储在 MySQL 数据类型中的以下 JSON json:
"users": [
{
"group": "manager",
"userID": "a123"
},
{
"group": "employee",
"userID": "a456"
}
]
Run Code Online (Sandbox Code Playgroud)
如何在"userID": "a456"不知道用户对象在数组中的位置的情况下删除它?
如果我们知道位置,那么可以进行以下操作:
$query = 'UPDATE jsontable SET
jsondata = JSON_REMOVE
(
jsondata,
"$.users[1]"
);';
Run Code Online (Sandbox Code Playgroud)
但是,我正在寻找的是这样的:
$query = 'UPDATE jsontable SET
jsondata = JSON_REMOVE
(
jsondata,
(
JSON_SEARCH
(
jsondata,
"all",
JSON_OBJECT("userID", "a456")
)
)
);';
Run Code Online (Sandbox Code Playgroud)
以上会产生该列的数据截断错误jsondata。其他变体(例如直接输入用户 ID)也会JSON_OBJECT产生错误。
我需要在查询中修复什么才能返回JSON_SEARCH我想要从数组中删除的特定对象的路径?
请记住,它应该只在 内部进行搜索users,因为主 JSON 对象上可能还有使用这些 ID 的其他属性。