CQM CQM - 6 months ago 9
PHP Question

How to format my data in php

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

Answer

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