2

myArray is:

Array
(
 [1] => 0
 [2] => 11970.99
 [3] => 2888
 [4] => 0
 [5] => 1500
 [6] => 0
 [7] => 0
 [8] => 0
 [9] => 0
 [10] => 0
 [11] => 0
 [12] => 0
)

I want to convert this into JSON, like:

[{"name":"Recebimentos","data":[0,11970.99,2888,0,1500,0,0,0,0,0,0,0]}

I tried:

echo json_encode(array(
 array(name=> 'Recebimentos', data=>$myArray),
 ));

But this is returning:

[{"name":"Recebimentos","data":{"1":0,"2":11970.99,"3":2888,"4":0,"5":1500,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}}
asked Apr 1, 2014 at 17:04
1
  • 3
    use array_values before encode Commented Apr 1, 2014 at 17:06

1 Answer 1

5

Your issue is non-sequencial indexes.
Php array must have indexes 0..array.length-1 to be encoded to JSON array.

You can reset array keys with array_values:

echo json_encode(array(
 array(name=> 'Recebimentos', data=>array_values($myArray)),
));
answered Apr 1, 2014 at 17:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.