Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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
如何从jQuery向PHP传递对象(或数组)?_Php_Javascript_Jquery - Fatal编程技术网

如何从jQuery向PHP传递对象(或数组)?

如何从jQuery向PHP传递对象(或数组)?,php,javascript,jquery,Php,Javascript,Jquery,我有一个数组,我正试图传递给正在传递的php文件,但它将数组显示为字符串。如何将字符串转换为数组,以便在php中使用它?这是我的密码: jQuery $('#add-child').click(function() { var _attributes = []; $('#attributes input').each(function() { _attributes.push($(this).val()); }); $.post('../a

我有一个数组,我正试图传递给正在传递的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\u dump()
,这是输出:

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

如果您想用逗号重新加入数组,那么

$attributes = implode(',' , json_decode($_POST['attributes']));

您根本不需要
JSON.stringify
,您可以使用

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

不是带有串联的
弹簧
为什么不按原样传递
\u属性
值?为什么要为它应用
JSON.stringify
?我试过了,在…中得到了一个错误>数组到字符串的转换,因为你在连接变量,假设它们在
print($department.:'.$category.:'.$attributes)行是字符串。而
$attributes
是一个数组。您将数据转换为JSON。现在您必须将其解析为PHP数组。@Felix Kling:。。或者删除转换(因为它毫无意义)嘿,阿提夫,我尝试了你的建议,我得到了>数组到字符串的转换…@Mike嘿,Mike,对不起,我不明白你说的话。你能解释一下吗clearly@Atif穆罕默德·阿梅努丁:他说他连接变量(字符串和数组)我想知道为什么数组不能转换成字符串:)@zerkms,但问题是“我如何将字符串转换成数组?我可以在php中使用它吗?”嘿,zerkms,我照你说的做了,并在我的帖子中列出了
var\u dump()
结果。@Mike:你得到了预期的结果。一切都正常,不是吗?我知道我是个白痴。。真正地我要连接一个字符串和数组,希望它能工作?新手犯的错误。也是的!当我执行以下
print($attributes[0])时,它会输出我的结果谢谢,干得好;)
var_dump($department, $category, $attributes);