0

I'm making a jQuery .post() with the following arrays:

'cleanedLinkStructureArray[]': cleanedLinkStructureArray,
'cleanedPermaLinkArray[]': cleanedPermaLinkArray

The data inside these arrays: cleanedPermaLinkArray looks like this: ["2012","10","30","hello-world"] and cleanedLinkStructureArray like this: ["year","monthnum","day","postname"]

Javascript code:

var ajaxPost = $.post(
            enableAJAX.ajaxurl, 
            { action: 'ajaxRequest',
            'ajaxRequestNonce' : enableAJAX.ajaxRequestNonce,
            'cleanedLinkStructureArray[]': cleanedLinkStructureArray,
            'cleanedPermaLinkArray[]': cleanedPermaLinkArray },
            'json'
    );

    ajaxPost.done(function(responseText) { 
        alert(responseText);
        console.log(responseText);
    }); 

    ajaxPost.fail(function() { 
        alert("Oops, I'm afraid we've broken something");
    });

I don't understand how I catch the two arrays in PHP? and use the data from the arrays inside PHP? Preferably I would create new PHP array with them, where the values inside cleanedLinkStructureArray become the keys for the array and the values inside cleanedPermaLinkArray the values for that new array.

I guess it must be something with this, but I need someone more experienced to tell me what I need to do here.

$_POST['cleanedPermaLinkArray[]']
$_POST['cleanedLinkStructureArray[]']; 

Any help would be appreciated.

Kind regards,

Marnix

4

3 回答 3

1

你的数组将在

$_POST['cleanedPermaLinkArray']
$_POST['cleanedLinkStructureArray']; 

你可以做一个简单var_dump($_POST)的看看数据是如何形成的

于 2012-11-10T02:20:43.067 回答
1

首先,在 $.post 中,您不需要方括号,因此这是您的参数之一:

'cleanedLinkStructureArray': cleanedLinkStructureArray,

然后在 PHP 中,您必须首先像这样捕获它:

$cleanedLinkStructureArray = $_POST["cleanedLinkStructureArray"];

现在,您可以使用以下内容:

foreach ($cleanedLinkStructureArray as $item) {
    // Do something with $item
}

另一种方法是将所有参数从 $.post 传递给 php 将它们设置为 json 对象。

于 2012-11-10T02:23:45.880 回答
0

This is all coming down from the server as JSON, right? Just use json_decode on the values and the'll be converted to arrays, natively inside of php.

$cleanedPermaLinkArray = json_decode($_POST['cleanedPermaLinkArray[]']);
echo cleanedPermaLinkArray[0]; // some value..
于 2012-11-10T02:15:09.490 回答