Sohil Desai
Sohil Desai

Reputation: 2988

php json response in ajax call

here is my php code

function GetTransactionList($data)
{
    // DB transaction   
    $records = array();
    while($row = mysql_fetch_array($result))
        array_push($records, $row);

    echo json_encode(array("IsError" => false, "Records" => $records));

}

this is my json response in ajax call from php

{
    IsError:false,
    Records:[
            {0:1,
             1:1000,
             2:0,
             3:"Peacock India trial payment",
             4:"2013-08-03",
             5:1,
             TransactionID:1,
             Credit:1000,
             Debit:0,
             Reason:"Peacock India trial payment",
             TransactionDate:"2013-08-03",
             TransactionByUserID:1
            }]
}

here I got my result but json_encode() method encodes each row twice first it sets values by index=>value pair and second time it encodes by column_name=>value pair. I want to know why it happens? Is there any way to reduce this double work.? I want json response only as following way

{
    IsError:false,
    Records:[
            {TransactionID:1,
             Credit:1000,
             Debit:0,
             Reason:"Peacock India trial payment",
             TransactionDate:"2013-08-03",
             TransactionByUserID:1
            }]
}

Upvotes: 3

Views: 154

Answers (2)

Murat Çorlu
Murat Çorlu

Reputation: 8545

You can use mysql_fetch_assoc instead of mysql_fetch_array.

Upvotes: 1

Musa
Musa

Reputation: 97672

No it doesn't, it is mysql_fetch_array that gives you both associate and numeric keys.
If you only want associative keys specify it in the function mysql_fetch_array($result, MYSQL_ASSOC);

Upvotes: 2

Related Questions