Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP复选框数组_Php_Json - Fatal编程技术网

PHP复选框数组

PHP复选框数组,php,json,Php,Json,全部, 发布表单时,我会得到一些复选框值,如下所示: [chk0] => Array ( [1] => on [57] => on [83] => on ) [chk1] => Array ( [69] => on [71] => on ) [chk1001] => on [chk1005] => on [chk1008] => on 使用PHP,如何使用上述变量

全部,

发布表单时,我会得到一些复选框值,如下所示:

 [chk0] => Array ( 
    [1] => on 
    [57] => on 
    [83] => on 
  ) 
 [chk1] => Array ( 
    [69] => on 
    [71] => on
 )
 [chk1001] => on 
 [chk1005] => on
 [chk1008] => on
使用PHP,如何使用上述变量构造一个包含两个数组的JSON请求?所有复选框的前缀均为“chk”。如果该复选框是一个数组并具有值,则应生成第一个JSON请求。对于所有不是数组的,它应该生成第二个数组

//JSON Request 1
       "data1":
            [
                {
                    "checkboxval": true,
                    "id": 1
                },
                {
                    "checkboxval": true,
                    "id": 57
                },
                {
                    "checkboxval": true,
                    "id": 83
                },
                {
                    "checkboxval": true,
                    "id": 69
                },
                {
                    "checkboxval": true,
                    "id": 71
                }
            ]

//JSON Request 2:
   "data2":
        [
            {
                "checkboxval": true,
                "id": 1001
            },
            {
                "checkboxval": true,
                "id": 1005
            },
            {
                "checkboxval": true,
                "id": 1008
            }
        ]

谢谢你的回答。但我认为这对第一个病例不起作用。谢谢你的回答。但我认为这对第一个案子不起作用。
$data = array(
    "data1" => array(),
    "data2" => array(),
);
foreach($_POST as $key => $value)
{
    if(preg_match('/^chk/',$key))
    {
        if(is_array($value))
        {
            foreach($value as $id => $on)
                $data["data1"][] = array("checkboxval"=>true,"id"=>$id);
        }
        else
            $data["data2"][] = array("checkboxval"=>true,"id"=>str_replace("chk","",$key));
    }
}
$json = json_encode($data);