0

I have an array in php, with print_r it looks like this

Array ( 
    [0] => Array ( [0] => 2 [1] => 3 [2] => 3 [3] => 1 [4] => 1 [5] => 4 [6] => 1 ) 
    [1] => Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 1 [4] => 1 [5] => 1 [6] => 1 ) 
) 

when I do json_encode($mydata) it looks like this

[["2","3","3","1","1","4","1"],["1","2","2","1","1","1","1"]]

but I need this without the quotes, as the next processing simply needs a dataset like this:

var dataset = [ 5, 10, 15, 20, 25 ];

the dataset was never intended to use json standards, I simply need to get my array formatted correctly.

so the question is either "how do I remove quotes from json" which is simply nonstandard invalid json and won't get me the responses I'd need

OR

"how do I format this php array to be like the dataset".

it either involves a different kind of print function in php, or it involves some forloop regex in javascript

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
CQM
  • 42,592
  • 75
  • 224
  • 366
  • 1
    possible answer: http://stackoverflow.com/questions/13938645/remove-double-quote-in-json-encode – pzirkind Jan 02 '13 at 20:59
  • 2
    Fix your data. Instead of adding strings to your array, add numbers. (numeric strings are shown like numbers in `print_r`: http://codepad.org/pXzFGWr9). – Felix Kling Jan 02 '13 at 21:04

5 Answers5

4

You need to use the JSON_NUMERIC_CHECK flag, this will force all numeric strings to be converted to numbers.

Like so:

json_encode($array,JSON_NUMERIC_CHECK);

Please note that this flag is only available from PHP version 5.3.3

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • Not quite correct. It makes sure that all *numeric strings* are converted to *numbers*. PHP won't convert any data to another data type be default, i.e. you don't have to "protect" your data. Quite a difference. And also: *Available since PHP 5.3.3.*. – Felix Kling Jan 02 '13 at 21:06
  • this did work for me, but my server is using php 5.2.x thank you @FelixKling saved me a potential headache – CQM Jan 02 '13 at 21:09
  • @FelixKling yes that is indeed correct, my mistake, will edit – Ben Carey Jan 02 '13 at 21:10
2

Ehhh you could try this:

$d = array();
foreach($myData as $data) {
    $d[] = "[" . implode(",", $data) . "]";
}
echo "[" . implode(",", $d) . "]";

Demo: http://codepad.org/nTveAGWm

Although echoing out json_encode($myData); worked as well: http://codepad.org/KTfQHz6s

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • thanks, I will probably be going with this, although the JSON_NUMERIC_CHECK worked for me, my server is using php 5.2.x so this answer will work better – CQM Jan 02 '13 at 21:10
  • 1
    @CQM: Again, because it's important: **Fix your data**. If you need numbers, use numbers. Rather map the strings in your array(s) to numbers and then use `json_encode` than manually create JSON. – Felix Kling Jan 02 '13 at 21:12
0

The reason json_encode is putting quotes is because the data is a string. Try this example to view the difference:

$arr = array(array(4,5,6), array(8,10, "11"));
print_r($arr);
echo "<br>";
echo json_encode($arr);

You can try to use intval to parse it as int before json_encoding

Pablo
  • 1,073
  • 8
  • 9
0

you could try this

$c = array(1,2,3);
echo "Non-associative array output: ", json_encode($c), "\n";

return this:
Non-associative array output: [1,2,3]

0

using your example string u can do smth like this

$arr = json_decode('[["2","3","3","1","1","4","1"],["1","2","2","1","1","1","1"]]');
foreach ($arr as &$val)
    foreach ($val as &$item)
        $item = (int)$item;
var_dump(json_encode($arr));

finally i get

string(33) "[[2,3,3,1,1,4,1],[1,2,2,1,1,1,1]]"
Nikita Platonenko
  • 702
  • 1
  • 7
  • 13