-2

I am exporting a array from PHP to JS to use a Pie Chart with AnyChart. But when i create a JS Array with PHP Data, JS Creates a Object instead of an array

This is a PHP Array

$ganhosArray[0] = array();
$ganhosArray[0]['x'] = 'Jhonatan';
$ganhosArray[0]['value'] = $valor01;

$ganhosArray[1] = array();
$ganhosArray[1]['x'] = 'Luiz';
$ganhosArray[1]['value'] = $valor02;

This is a JS Object,but i want to create an array.

var users = ". json_encode($ganhosArray).";
  • 1
    Have a look at this post, your question was already answered in there https://stackoverflow.com/questions/11195692/json-encode-sparse-php-array-as-json-array-not-json-object – Habebit Mar 03 '19 at 18:50
  • ‘Object.entries(users)’ should do that – theBuzzyCoder Mar 03 '19 at 18:51

1 Answers1

0

json_encode(array_values($ganhosArray))

json_encode sparse PHP array as JSON array, not JSON object

Pablo Castro
  • 134
  • 6