用于打印的数组为json,array和xml

时间:2013-02-15 17:42:29

标签: php xml json

我正在尝试以json格式获得正确的输出,但下面的输出有点乱。它应该是这样的: "{"table":"users","operation":"select","username":"inan"}"

如何解决我的问题?

由于

server.php

print_r($_POST);

client.php

$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl_handle);

if ($this->request_response_type == 'array')
{
echo $output;
}
else if ($this->request_response_type == 'json')
{
echo json_encode($output);
}
else if ($this->request_response_type == 'xml')
{
    //xml conversion will be done here later. not ready yet.
}

输出:

"Array\n(\n    [table] => users\n    [operation] => select\n    [uid] => yoyo\n)\n"

2 个答案:

答案 0 :(得分:2)

使用 print_r 打印出的数组无法解析为变量。

在您的server.php中echo json_encode($_POST);。 然后在你的client.php

<?php
//...
$output = curl_exec($curl_handle);

// and now you can output it however you like
if ($this->request_response_type == 'array')
{
    //though i don't see why you would want that as output is now useless if someone ought to be using the variable
    $outputVar = json_decode($output); // that parses the string into the variable
    print_r((array)$outputVar);
    // or even better use serialize()
    echo serialize($outputVar);
}
else if ($this->request_response_type == 'json')
{
    echo $output; //this outputs the original json
}
else if ($this->request_response_type == 'xml')
{
    // do your xml magic with $outputVar which is a variable and not a string
    //xml conversion will be done here later. not ready yet.
}

答案 1 :(得分:-1)