如何将包含复选框值的json字符串转换为列表?
例如,我有以下json字符串: {"用户1":"经过""用户2":"""用户3":"检查"}
我想从上面的数据中提取一个列表,并将其分配给变量。列表当然应该只输出已检查的元素:
User1的 用户3
并忽略未经检查的项目。
这可能吗?
最初的想法是在jqgrid布局中做到这一点,原始问题在这里: https://stackoverflow.com/questions/34615244/best-way-to-do-jqgrid-with-json-strings
全数组代码(处理方):
$status = $data['data-invoice-status']; // this is an array
$status_array = array(); // create new array
$status_names = array(1 => "Service Call",2 => "Estimate",3 => "Bid Accepted",4 => "Unstaged",5 => "Staged",6 => "Assigned",7 => "In Progress",8 => "Job Complete",9 => "Billed");
for($i = 1; $i <= 9; $i++){ // loop from 1 to 5
if(in_array($i, $status)){ // if value exists (has been selected), stack 'checked', if not, stack ''.
$status_array[$status_names[$i]] = "checked";
} else {
$status_array[$status_names[$i]] = "";
}
}
$status_json = mysqli_real_escape_string($this->_con, json_encode($status_array)); // encode the array into JSON and then escape it.
每当有人更新表单时,我都会将$ statusd保存到我的数据库中,但不会传输任何数据。
答案 0 :(得分:4)
将JSON解码为数组,然后获取值等于&#39; Checked&#39;:
的键$list = array_keys(json_decode($json, true), 'Checked');
或者可能过滤掉空白的商品而不是搜索“已选中”,但是您仍然有键而不是值:
$list = array_filter(json_decode($json, true));
然后implode()
或foreach()
或其他任何可以使用数组。
mysqli_real_escape_string
,然后尝试再次解码假设你不打算做#1那么就是这样:
$statusd = mysqli_real_escape_string($this->_con,
implode(",", array_keys($status_array, 'Checked')));