-2

書きたいのは、データベースのジェイソン文字列に格納される配列である文字列です。私のコードはチェックボックスを繰り返しますが、$input名が"interests"であるかどうかをテストできるようにしたいです

<input type="checkbox" name="interests[]" value="dvd" />` <-- checkbox lists

私が得ることができない他のことは、例えば「dvd」、「computers」のように各$valueの周りに引用符を置くことです

$interests = '[';
$count = 1;
$counter = count($_POST["interests"]);
foreach($_POST as $checkbox => $input) {
    if(is_array($input)) {
    // test here is input is "interests"    
        foreach($input as $index => $value) {   
            $interests .= /*quote here*/ $value /*quote here*/ .= ($count < $counter) ? ',' : '';
            $count += 1;
        }
    }
}
$interests .= ']';
echo $interests;

興味は["dvd"、 "computers"、 "hard drive"]を書き出すことを想定していますが、それは[dvd、computers、harddrives]だけを書き出すだけです。

4

3 に答える 3

1
$_POST["interests"] = array("dvd", "computers", "hard drives");
$interests = '["' . implode('","', $_POST["interests"]) . '"]';
echo $interests;

実際に見てください

于 2013-02-08T07:12:23.443 に答える
0

これを試して、

$interests = '';
$count = 1;
$counter = count($_POST["interests"]);
foreach($_POST as $checkbox => $input) {
    if(is_array($input)) {
    // test here is input is "interests"    
        foreach($input as $index => $value) {   
            $interests .= $value .= ($count < $counter) ? ',' : '';
            $count += 1;
        }
    }
}
$interests = json_encode($interests);
echo $interests;
于 2013-02-08T07:17:29.397 に答える
0

json_encode()JSONを手動で作成する代わりに使用します。

echo json_encode($_POST['interests']);

出力

["dvd"、 "tv"、 "radio"]

于 2013-02-08T07:12:47.040 に答える