【发布时间】:2016-01-05 18:26:46
【问题描述】:
如何将包含复选框值的 json 字符串转换为列表?
例如,我有以下 json 字符串: {"User1":"Checked","User2":"","User3":"Checked"}
我想从上面的数据中提取一个列表,并将其分配给一个变量。该列表当然应该只输出选中的元素:
用户 1 用户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 保存到我的数据库中。
【问题讨论】: