tags:

views:

190

answers:

5

All,

When a form gets posted, I get some checkbox values like the below:

Array ( [chk0] => true ,
        [chk1] => true,
        [chk3] => true,
        [chk1002] => on,
        [chk1005] => on 
      )

Using PHP,How can I construct a JSON request like this by using the above variables?

        "data":
        [
            {
                "checkboxval": true,
                "id": 0
            },
            {
                "checkboxval": true,
                "id": 1
            },
            {
                "checkboxval": true,
                "id": 3
            },
            {
                "checkboxval": true,
                "id": 1002
            },
            {
                "checkboxval": true,
                "id": 1005
            }
        ]

Please note that my POST variables can have other form variables too, but all checkbox values will be named with the prefix "chk"

Thanks

A: 

Have a look at the json_encode() php function. You'll have to massage your array a little to get the exact JSON format you're after.

Asaph
I am not so much worried for the JSON format..I am interested in the logic for scanning through the dynamically generated checkbox values which have a prefix of chk.
Vincent
@Vincent Have a look at @Cletus' answer.
Asaph
Thanks..But $output[] array(...) seems to be syntactically wrong..
Vincent
@Vincent: I made a minor correction to @cletus' answer. Try it now.
Asaph
Thanks Asaph :)
Vincent
A: 

Heres an example...

$_POST["chk1"] = "Hello";
$_POST["chk2"] = "World";
$jsonArray = array();
foreach($_POST as $key => $val){
  if(preg_match("/chk/", $key)){
    $jsonArray[$key] = $val;
  }
}
$jsonArray = array("Data" => $jsonArray);
$json = json_encode($jsonArray);
echo "<pre>";
echo $json;
echo "</pre>";

Outputs this:

{"Data":{"chk1":"Hello","chk2":"World"}}
Greg W
+1  A: 
$output = array();
foreach ($input as $k => $v) {
  $output[] = array(
    'checkboxval' => !!$v,
    'id' => preg_replace('!^chk!', '', $k),
  );
}
header('Content-Type: application/json');
echo json_encode(array('data' => $output));
cletus
How do I get all the posted checkboxes in $input here?
Vincent
I guess I can use $_POST for $input.. However, $output[] array(...) seems to be syntactically wrong..
Vincent
@Vincent: It looks like @cletus left out an equals sign. I added it in for him.
Asaph
+1  A: 
foreach ($_POST as $k => $v) {
  $output[] = array(
    'checkboxval' => ($v=='on'? true : ($v=='off' ? false : !!$v)),
    'id' => preg_replace('!^chk!', '', $k),
  );
}
header('Content-Type: application/json');
echo json_encode(array('data' => $output));

Credits to cletus who provided the basis for this code.

Peter Lindqvist
A: 

I haven't tested this yet, but maybe something like this:

$json = '"data": [';
$first = true;
foreach ($_POST as $k => $v){
    if (preg_match('/^chk(\d+)$/', $k, $m)){
     if ($first) $first = false; else $json .= ", ";
     $json .= sprintf('{ "checkboxval" : %s, "id" : %s }', ($v && $v != "off") ? "true" : "false", $m[1]);
    }
}
$json .= ']';
MiffTheFox