1

i have a php array

if(mysql_affected_rows() > 0)
{
    $status = "OK";

    while($row = mysql_fetch_array($result)) {

        $info[] = array( 
                'postDate' => $row[1],
                'vedioURL' => extractifrem($row[2]),
                'like' => $row[3],  
                'totalView' => $row[4], 
                'viewTime' => $row[5]                   
            );

    }

$output = array(
            'status' => $status,
            'result' => $info
        );

I want to print my result of $output as valid json readable.I am using this code but not work

$soutput = array();
foreach($output as $v) {
    $soutput[key($v)] = current($v);
}
echo json_encode($soutput, 128);
2
  • why dont' you use echo json_encode($output, true); directly Commented Feb 7, 2014 at 6:44
  • I need the formate as "status": "OK", "result": [ { "postDate": "2013-08-27 13:16:35", "vedioURL": false, "like": "0", "totalView": null, "viewTime": null }, { "postDate": "2013-08-27 13:16:36", "vedioURL": false, "like": "0", "totalView": null, "viewTime": null }]@SatishSharma Commented Feb 7, 2014 at 6:47

2 Answers 2

4

Do like this..

$soutput = array();
foreach($output as $k=>$v) {
    $soutput[$k] = $v;
}
echo json_encode($soutput);
Sign up to request clarification or add additional context in comments.

2 Comments

It returns Warning: key() expects parameter 1 to be array and Warning: current() expects parameter 1 to be array
is there any way to do result format as "status": "OK", "result": [ { "postDate": "2013-08-27 13:16:35", "vedioURL": false, "like": "0", "totalView": null, "viewTime": null }, { "postDate": "2013-08-27 13:16:36", "vedioURL": false, "like": "0", "totalView": null, "viewTime": null }]
0

Use only json_encode($soutput, true) inplace of json_encode($soutput, 128);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.