jQuery ajax对象数组php

I have:

var apiQuizData = {'ect stuff removed...',answers:{}};
    $.each(dataCEActiveQuiz.quiz_data, function(index, answer) {
        if(answer.selected == undefined){
            apiQuizData.answers[answer.id] = 0;
        } else {
            apiQuizData.answers[answer.id] = answer.selected;
        }
    });

    $.post(URL, apiQuizData, function(data) {

If I look at the form data submitted through the header via chromes inspect tools it shows:

// url decoded
answers[28194]:112768
answers[28195]:112773
answers[28199]:112788
answers[28202]:112803
answers[28204]:112809

// url encoded
answers%5B28194%5D:112768
answers%5B28195%5D:112773
answers%5B28199%5D:112788
answers%5B28202%5D:112803
answers%5B28204%5D:112809

// query string

answers%5B28195%5D=112773&answers%5B28199%5D=112788&answers%5B28202%5D=112803&answers%5B28204%5D=112809

In PHP I use

$sent_data = file_get_contents('php://input');
$sent_data_decoded = json_decode($sent_data, true);

the string that php receives is

&answers=&answers=&answers=&answers=&answers=

What do I need to do to the data so that it goes through to php with the values?

Thanks.

=================

UPDATE 1

If I use

$.post(URL, JSON.stringify(apiQuizData), function(data) {

This is what is sent

{...extra stuff...,"answers":{"28195":"112773","28199":"112791","28201":"112796","28202":"112800","28204":"112810"}}

From PHP using json_decode(file_get_contents('php://input'), true);

{...extrastuff...}id%22%3A952077%2C%22answers%22%3A%7B%2228195%22%3A%22112

When I do a print_r of the data it is an empty array?

=================

UPDATE 2 - Working

Updated the jquery post to

    $.post(URL + 'sendCEQuizResults', {jsonstringify: JSON.stringify(apiQuizData)}, function(data) {

Updated the php receiving code to handle the new way I am sending data with the old way

$sent_data = file_get_contents('php://input');

            if(substr($sent_data, 0, 13) == 'jsonstringify')
            {
                parse_str($sent_data);
                $sent_data_decoded = json_decode($jsonstringify, true);
            } else
            {
                $sent_data_decoded = json_decode($sent_data, true);
            }

For some reason it would not work if I didn't assign the JSON.stringify(apiQuizData) into the value of another object. The browser seemed to choke on the text by itself, I guess because it was a huge text string by itself? not sure. Either way the above update #2 solved the issues I was having.

Thanks.

Stringify the object into a JSON string:

$.post(URL, JSON.stringify(apiQuizData), function(data) {

Before I answer your question I would like to encourage you to follow the "Ask Question" recommendations in order to facilitate people willing to help you all they need to know to answer your question. Yours has been too ambiguous and I came to understand what you need with some difficulty.


You may want to use php parse_str function for this:

<?php
    $str = "first=value&arr[]=foo+bar&arr[]=baz";
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz

    parse_str($str, $output);
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz

?>