2

渡すphpファイルに渡そうとする配列がありますが、配列が文字列として表示されます。文字列を配列に変換してphpで処理できるようにするにはどうすればよいですか? これが私のコードです:

jQuery

$('#add-child').click(function()
{
    var _attributes = [];
    $('#attributes input').each(function()
    {
        _attributes.push($(this).val());
    });
    $.post('../assets/hub.php',{'task':'add-child','parent':$('#parent-id').val(),'value':$('#child-value').val(),'attributes':JSON.stringify(_attributes)},function(callback)
    {
        console.log(callback);
    });
});

PHP

case 'add-child':
    $department = $_POST['parent'];
    $category   = $_POST['value'];        
    $attributes = $_POST['attributes'];
    print($department.' : '.$category.' : '.$attributes);
break;

電流出力

test1 : test2 : ["sdfg","34","vsdf"] 

** 編集 **

私は削除JSON.stringify()して追加var_dump()しました。これが出力です:

string(3) "114"
string(4) "asdf"
array(3) {
    [0]=>
    string(4) "asdf"
    [1]=>
    string(4) "ZVCX"
    [2]=>
    string(5) "asdfr"

}

4

2 に答える 2

1

まったく必要なくJSON.stringify、次を使用してすべてが正常に渡されたことを確認します

var_dump($department, $category, $attributes);

pring_rwith 連結ではありません

于 2012-11-18T21:44:21.250 に答える
1
$attributes = json_decode($_POST['attributes']);

配列をコンマで元に戻す場合は、

$attributes = implode(',' , json_decode($_POST['attributes']));
于 2012-11-18T21:40:11.047 に答える