0

I am querying my DB, looping through the results and then converting to JSON. I am also using array_unique function.

The problem is my JSON file includes the indexes, how can I return in JSON format without the indexes? I need it this way as I am then planning to feed the JSON array directly into an apexcharts.js chart.

Here is my code:

$month = [];
$value = [];
foreach($rows as $row) {
    
    $month[] = $row['Source'];
    $value[] = $row['Days_To_Report'];
}
$month1 = array_unique($month);
$month_json = json_encode($month1, JSON_PRETTY_PRINT);
$fp = fopen('value.json', 'w');
fwrite($fp, $month_json);
fclose($fp);

And here is the JSON output:

{
    "0": "CGI Insurance",
    "4": "THIRD PARTY CAPTURE - CGI"
}

What I need is this:

{
    "CGI Insurance",
    "THIRD PARTY CAPTURE - CGI"
}

How can I do this? Thanks

3
  • 4
    you need array_values Commented Feb 9, 2022 at 16:34
  • 2
    Unrelated but file_put_contents is a lot easier to use than open, write, close. Commented Feb 9, 2022 at 16:38
  • Which dbms are you using? And show us your current SQL query attempt. Commented Feb 9, 2022 at 16:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.