2014-02-10 35 views
0

我有一个Javascript数组通过Ajax传递给PHP脚本。如何将此PHP对象转换为数组?

file.js

var params = {}; 
params["apples"] = "five"; 
params["oranges"] = "six"; 
params["pears"] = "nine"; 
var ajaxData = {data : params}; 
fetchData(ajaxData); 

function fetchData(arg) { 
    $.ajaxSetup ({ 
     cache: false 
    }); 

    request = $.ajax ({ 
     type: "GET", 
     url: "script.php", 
     data: arg, 
    }); 


    request.done(function(response){ 
      $("#somediv").text(response); 
    }); 

    request.fail(function (jqXHR, textStatus, errorThrown){ 
     // log the error to the console 
     console.error(
      "The following error occured: "+ 
      textStatus, errorThrown 
     ); 
    }); 
} 

的script.php

<?php 
    $data = json_decode(stripslashes($_GET['data'])); 
    var_dump($data); 
?> 

后续代码var_dump的结果是:

object(stdClass)#1 (3) { ["apples"]=> string(4) "five" ["oranges"]=> string(3) "six" ["pears"]=> string(4) "nine" } 

但我不要使用一个对象,我想使用它他同样的方式我用JavaScript中的数据(即:能够做到:

$apples = $data["apples"] 

有什么办法来处理这个数据作为数组,而不是一个对象从一开始走?如果没有,我该如何将我现在的东西“转换”到与Javascript中相同的关联数组中?

谢谢。

+0

这不是一个Javascript数组。 –

回答