多维数组到json对象php

时间:2016-08-10 09:13:34

标签: php arrays json multidimensional-array

我尝试使用json_encode,但结果结果类似于json格式的数组,任何人都可以帮助我形成json对象。我从一个网址获取数组我需要直接将其转换为json

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $Url);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $qs);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$array = curl_exec($c);

这是我从打印网址获得的数组

Array(
[status] => 1
[msg] => 2 vo settled on 2015-05-07
[Vo_details] => Array
    (
        [0] => Array
            (

                [void] => 12sd
                [vdate] => 2015-05-07 13:04:11
                [mode] => DD
                [amount] => 10.00
                [bank_name] => DD
                [issuing_bank] => abc
            )

        [1] => Array
            (

                [void] => 12a
                [vdate] => 2015-05-07 15:10:18
                [mode] => DD
                [amount] => 10.00
                [bank_name] => EE
                [issuing_bank] => abc
            )

    ))

如果我使用json_encode我得到这样的东西

数组\ n(\ n [status] => 1 \ n [msg] => 2 vo在2015-05-07结算\ n [Vo_details] =>数组\ n(\ n [0] =>数组\ n(\ n [void] => 12sd \ n [vdate] => 2015-05-07 13:04:11 \ n [模式] => DD \ n [金额] =&gt ; 10.00 \ n [bank_name] => DD \ n [issuing_bank] => abc \ n)\ n \ n [1] =>数组\ n(\ n [void] => 12a \ n [void ] => 2015-05-07 15:10:18 \ n [模式] => DD \ n [金额] => 10.00 \ n [bank_name] => EE \ n [issuing_bank] => abc \ n)\ n)\ n \ n)\ n< / pre>“

3 个答案:

答案 0 :(得分:0)

$json_var = json_encode($your_array);

答案 1 :(得分:0)

请尝试:

$array = Array(
'status' => 1,
'msg' => '2 vo settled on 2015-05-07',
'Vo_details' => Array
    (
        '0' => Array
            (

                'void' => '12sd',
                'vdate' => '2015-05-07 13:04:11',
                'mode' => 'DD',
                'amount' => '10.00',
                'bank_name' => 'DD',
                'issuing_bank' => 'abc'
            ),

        '1' => Array
            (

                'void' => '12a',
                'vdate' => '2015-05-07 15:10:18',
                'mode' => 'DD',
                'amount' => '10.00',
                'bank_name' => 'EE',
                'issuing_bank' => 'abc'
            )

    ),
    );

echo json_encode($array , JSON_FORCE_OBJECT);

<强>输出

{
   "status":1,
   "msg":"2 vo settled on 2015-05-07",
   "Vo_details":{
      "0":{
         "void":"12sd",
         "vdate":"2015-05-07 13:04:11",
         "mode":"DD",
         "amount":"10.00",
         "bank_name":"DD",
         "issuing_bank":"abc"
      },
      "1":{
         "void":"12a",
         "vdate":"2015-05-07 15:10:18",
         "mode":"DD",
         "amount":"10.00",
         "bank_name":"EE",
         "issuing_bank":"abc"
      }
   }
}

答案 2 :(得分:0)

该数组看起来像是print_r函数的结果 print_r输出没有解析器。创建该代码的人必须将print_r更改为echo json_encode()