将PHP stdClass解码为字符串

时间:2015-05-17 15:37:38

标签: php json string decode

我的.txt文件包含我的php文件的输出:

    stdClass::__set_state(array(
   'Months' => 
  array (
    0 => 'Janeiro',
    1 => 'Fevereiro',
    2 => 'Março',
    3 => 'Abril',
    4 => 'Maio',
    5 => 'Junho',
    6 => 'Julho',
    7 => 'Agosto',
    8 => 'Setembro',
    9 => 'Outubro',
    10 => 'Novembro',
    11 => 'Dezembro',
  ),
   'Cultures' => 
  array (
    0 => 'Algodão',
    1 => 'Amendoim',
    2 => 'Arroz',
    3 => 'Banana',
    4 => 'Batata',
    5 => 'Beterraba',
    6 => 'Cana-de-açucar',
    7 => 'Cártamo',
    8 => 'Cebola',
    9 => 'Citrinos',
    10 => 'Couve',
    11 => 'Ervilha',
    12 => 'Feijão',
    13 => 'Feijão-verde',
    14 => 'Girassol',
    15 => 'Luzema',
    16 => 'Melancia',
    17 => 'Milho',
    18 => 'Oliveira',
    19 => 'Pimento',
    20 => 'Soja',
    21 => 'Sorgo',
    22 => 'Tabaco',
    23 => 'Tomate',
    24 => 'Trigo',
    25 => 'Vinha',
  ),
   'Coeficients' => 
  array (
    0 => 0.7760000000000000230926389122032560408115386962890625,
    1 => 0.7119999999999999662492200513952411711215972900390625,
    2 => 1.3000000000000000444089209850062616169452667236328125,
    3 => 0.85999999999999998667732370449812151491641998291015625,
    4 => 0.7660000000000000142108547152020037174224853515625,
    5 => 0.786000000000000031974423109204508364200592041015625,
    6 => 0.745999999999999996447286321199499070644378662109375,
    7 => 0.61199999999999998845368054389837197959423065185546875,
    8 => 0.810000000000000053290705182007513940334320068359375,
    9 => 0.7600000000000000088817841970012523233890533447265625,
    10 => 0.7800000000000000266453525910037569701671600341796875,
    11 => 0.88000000000000000444089209850062616169452667236328125,
    12 => 0.62199999999999999733546474089962430298328399658203125,
    13 => 0.77400000000000002131628207280300557613372802734375,
    14 => 0.7359999999999999875655021241982467472553253173828125,
    15 => 0.71499999999999996891375531049561686813831329345703125,
    16 => 0.75,
    17 => 0.72599999999999997868371792719699442386627197265625,
    18 => 0.5,
    19 => 0.7600000000000000088817841970012523233890533447265625,
    20 => 0.7399999999999999911182158029987476766109466552734375,
    21 => 0.81799999999999994937383007709286175668239593505859375,
    22 => 0.7880000000000000337507799486047588288784027099609375,
    23 => 0.7600000000000000088817841970012523233890533447265625,
    24 => 0.74199999999999999289457264239899814128875732421875,
    25 => 0.65000000000000002220446049250313080847263336181640625,
  ),
   'TypeofWatering' => 
  array (
    0 => 'Faixas',
    1 => 'Canteiros',
    2 => 'Sulcos',
    3 => 'Gota-a-gota',
    4 => 'Miniaspersão',
    5 => 'Aspersão',
  ),
))

但是我需要循环来获取其中的数组值,我该如何实现呢?这是json_decode对js对象进行字符串化的结果。提前谢谢。

编辑:

我有这个js对象:

this.fields = {
      "Months": ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
      "Cultures": ["Algodão","Amendoim","Arroz","Banana","Batata","Beterraba","Cana-de-açucar","Cártamo","Cebola","Citrinos","Couve","Ervilha","Feijão","Feijão-verde","Girassol","Luzema","Melancia","Milho","Oliveira","Pimento","Soja","Sorgo","Tabaco","Tomate","Trigo","Vinha"],
      "Coeficients": [0.776,0.712,1.3,0.86,0.766,0.786,0.746,0.612,0.81,0.76,0.78,0.88,0.622,0.774,0.736,0.715,0.75,0.726,0.5,0.76,0.74,0.818,0.788,0.76,0.742,0.65],
      "TypeofWatering": ["Faixas","Canteiros","Sulcos","Gota-a-gota","Miniaspersão","Aspersão"]
};

然后我:

$.ajax({
          url: 'http://localhost:4000/process-data.php',
          type: 'post',
          data: { 'input-data': JSON.stringify(this.fields) }

最后在我的php文件中:

    $data = json_decode($_POST['input-data']);

$formattedData = var_export($data, true);

1 个答案:

答案 0 :(得分:2)

您可以像通过数组一样遍历stdclass对象。

foreach($obj as $k => $v)
  // ToDo

如果您希望它作为数组,您可以轻松地将对象属性(如果stdclass只有属性)转换为数组:

$arr = (array) $obj;