Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit my json to not include the row numbers

php to connect and get all rows

$query = 'SELECT * FROM cdr'; 
$sql = $remotedbh->prepare($query);  
$sql->execute(); 
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC); 

if($sql->rowCount() > 0){ 
    header('Content-type: application/json');
    echo json_encode($dblist,JSON_FORCE_OBJECT); 
} 
else {
    echo 0; 
}  

The problem is my json looks like this

{
"0": {
    "calldate": "2013-08-14 11:41:28",
    "clid": "\"tet\" <1002>",
    "src": "1002",
    "dst": "8834404",
    "dcontext": "from-internal",
    "channel": "SIP\/1002-00000000",
    "dstchannel": "IAX2\/voipms-6749",
    "lastapp": "Dial",
    "lastdata": "IAX2\/voipms\/14798834404,300,",
    "duration": "7",
    "billsec": "0",
    "disposition": "NO ANSWER",
    "amaflags": "3",
    "accountcode": "",
    "uniqueid": "1376498488.1",
    "userfield": "",
    "did": "",
    "recordingfile": "",
    "cnum": "",
    "cnam": "",
    "outbound_cnum": "",
    "outbound_cnam": "",
    "dst_cnam": ""
},
"1": {
    "calldate": "2013-08-14 11:42:55",
    "clid": "\"Rtest\" <1002>",
    "src": "1002",
    "dst": "9187755592",
    "dcontext": "from-internal",
    "channel": "SIP\/1002-00000001",
    "dstchannel": "IAX2\/voipms-121",
    "lastapp": "Dial",
    "lastdata": "IAX2\/voipms\/19187755592,300,",
    "duration": "494",
    "billsec": "485",
    "disposition": "ANSWERED",
    "amaflags": "3",
    "accountcode": "",
    "uniqueid": "1376498575.3",
    "userfield": "",
    "did": "",
    "recordingfile": "",
    "cnum": "",
    "cnam": "",
    "outbound_cnum": "",
    "outbound_cnam": "",
    "dst_cnam": ""
},
"2": {
       so on so forth

I also am having the problem of everytime it pulls all the information a row is only half complete so it has a problem with that aswell, I was thinking of finding away to query the everything that is 5 minutes or older.

like image 839
user1552172 Avatar asked Nov 19 '25 16:11

user1552172


2 Answers

It's including the row numbers as you're using JSON_FORCE_OBJECT

Remove it and it it'll successfully return as an array.

e.g.

echo json_encode($dblist); 
like image 60
Doug Avatar answered Nov 22 '25 06:11

Doug


JSON_FORCE_OBJECT option in json_encode() forces the array to a json object with its indexes as keys. Removing that might get working for you.

update :

because while testing with a sample array

$test = array(array("calldate"=> "2013-08-14","src"=> "1002"),
              array("calldate"=> "2013-08-18","src"=> "1003")
        );

echo json_encode($test,JSON_FORCE_OBJECT); 
//Outputs :
//{"0":{"calldate":"2013-08-14","src":"1002"},"1":{"calldate":"2013-08-18","src":"1003"}}

and

echo json_encode($test);
//Outputs :
// Gave me [{"calldate":"2013-08-14","src":"1002"},{"calldate":"2013-08-18","src":"1003"}]

so using just

echo json_encode($dblist);

should do the trick

like image 38
Mithun Satheesh Avatar answered Nov 22 '25 05:11

Mithun Satheesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!