-2

I tried using json_encode but the result ends up something similar to array not in json format can any one help me in forming the json object pls. im getting the array from an url i need to convert it to json directly

$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);

this is the array i get from printing the url

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
            )

    ))

if i use json_encode im getting something like this

Array\n(\n [status] => 1\n [msg] => 2 vo settled on 2015-05-07\n [Vo_details] => Array\n (\n [0] => Array\n (\n [void] => 12sd\n [vdate] => 2015-05-07 13:04:11\n [mode] => DD\n [amount] => 10.00\n [bank_name] => DD\n [issuing_bank] => abc\n )\n\n [1] => Array\n (\n [void] => 12a\n [void] => 2015-05-07 15:10:18\n [mode] => DD\n [amount] => 10.00\n [bank_name] => EE\n [issuing_bank] => abc\n )\n )\n\n)\n</pre>"

sano
  • 1
  • 2

3 Answers3

0
$json_var = json_encode($your_array);
Ghassen Rjab
  • 683
  • 7
  • 20
0

Please try this:

$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);

Output

{
   "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"
      }
   }
}
Manish Jesani
  • 1,339
  • 5
  • 20
  • 36
  • i did still i cant convert it into json string – sano Aug 10 '16 at 09:44
  • @sano i convert your array in json string Please see my answer i get this outout. any problem tell me – Manish Jesani Aug 10 '16 at 09:48
  • if u give the array in the form i too get the same answer but im getting the values from url which provides the values like i have given in the qn can you do any thing abt it – sano Aug 10 '16 at 09:52
  • @sano I cant understant please explain detail – Manish Jesani Aug 10 '16 at 09:58
  • im getting the array values from an url. i need to convert the array into json string. if i use json_encode im getting something else not the json string – sano Aug 10 '16 at 10:03
  • @sano This string call json string. if you convert in array use json_decode() – Manish Jesani Aug 10 '16 at 10:07
  • Manish, Thanks for the response. But, there seems to be a small change in Array you have considered and the one i have considered. In my Array, the variables are wrapped by square brackers [] and you have considered the variables to be wrapped by single qoutes ''. Please let me know if there is a way to get the desired output considering the variables are wrapped by []. Thanks – sano Aug 10 '16 at 10:25
  • @sano Why you using foreach. Please explain – Manish Jesani Aug 10 '16 at 10:54
  • http://stackoverflow.com/questions/6413589/php-foreach-with-multidimensional-array they have done using for each and so i thought it might help – sano Aug 10 '16 at 11:22
0

That array looks like a result of print_r function.
There is no parser for print_r output. Whoever created that code has to change print_r to echo json_encode().

Naktibalda
  • 13,705
  • 5
  • 35
  • 51