Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am querying a table that return to fields(message_type & percentage). I use PHP to encode the json data, here is how i do it

$json = array();
while ($row = odbc_fetch_array($rs)) {
  $json[][] = $row;
}
echo json_encode($json);

output :

[ [ { "message_type" : "bullying",
      "percentage" : "60"
    } ],
  [ { "message_type" : "cheating",
      "percentage" : " 14"
    } ],
  [ { "message_type" : "Stress",
      "percentage" : "16"
    } ],
  [ { "message_type" : "Gang",
      "percentage" : "7"
    } ]
]

As you can see json_encode function is adding curly braces, quotes and the object key name.

What I want is to parse the json as two dimensional array only, here is the desired output:

[
  ["bullying", 60],
  ["harrassment", 9],
  ["cheating", 14],
  ["Stress", 16],
  ["Gang", 7]
]

I also tried to encode it manually but I could not get the result I need.

share|improve this question
3  
$json[][] --- why [][]? –  zerkms Apr 9 '13 at 23:22
5  
$json[] = array_values($row); –  DaveRandom Apr 9 '13 at 23:23
    
@DaveRandom you solved my problem if you add it as an answer I will accept –  meda Apr 9 '13 at 23:27

1 Answer 1

up vote 5 down vote accepted

PHP's json_encode() uses a certain amount of magic to determine whether a given vector is encoded as a JSON object or an array, but the simple rule is this: If the array has contiguous, zero-indexed, numeric keys, it will be encoded as an array. Any other vector (object or associative array) will be encoded as an object.

Because you are using odbc_fetch_array(), your result rows are returned as an associative array with the keys being the column names. To get the result you want you have 3 options:

Pass the result rows through array_values():

$json[] = array_values($row);

Manually construct the individual arrays:

$json[] = array($row['message_type'], $row['percentage']);

Or probably the best option is to use odbc_fetch_row() instead, which will return indexed arrays straight away:

while ($row = odbc_fetch_row($rs)) {
    $json[] = $row;
}
share|improve this answer
    
Thanks this solved my problem , thanks for the detail answer. I also noticed json_encode remove the leading zero if the value is less than 1, for example 0.23 ---> .23 how do I avoid that? –  meda Apr 9 '13 at 23:42
    
@meda That wouldn't happen if the data was a float and json_encode() wouldn't do it at all, I suspect it's odbc that's doing that (although it's pretty odd behaviour for any environment). Can you show a var_dump($json); just before the json_encode() call? –  DaveRandom Apr 9 '13 at 23:44
    
here is one of the array before the encoding [12]=>array(2) { [0]=> string(17) "Sexual Harrasment[1]=>string(12) ".16638935100"} –  meda Apr 10 '13 at 0:13
    
@meda Yes, you can see that the leading zero has already been stripped off before it is passed to JSON. What is the data type of the percentage column in the database? –  DaveRandom Apr 10 '13 at 1:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.