Avatar of B O
B O asked on

How do I prevent a json array I a am creating from repeating itself

Hi there,

Thank you for checking out my problem I am facing now,
I wont drag this to long, so straight to bisness.

I am trying to create a json file as following for a chart I am creating using highCHarts:\

{name: "2019",
data: [4239042384.42, 3940280.60, 762345.70, 230947873.34]},
{name: "2020",
data: [239042384790,85, 932043289048.85, 428739462,98]},
data: [809273502.95, 8792634593486.85, 893475893475.85]}

Open in new window

current view:

$q_res_sales_per_month = $oDbConn->selectAll($_sel);

$month_array = array();

$current_year  = '';
$current_year_parse = '';

$single = '';

$single_months = '';
$single_month_parse = [];

$year_month_data = array();

//load every month in dataset
foreach($q_res_sales_per_month as $key => $val) {

//    unset($current_year_parse);
    $current_year = implode("-", $val);
    $current_year_parse = explode("-", $current_year);

    //add year of element in array $year_month_array if not defined yet
    if(!in_array(substr($current_year_parse[2], 0, 4, ), $year_month_data)){
        // load checked year to array
        $month_array[$key]['name'] .= json_decode($year_month_data[] = substr($current_year_parse[2], 0, 4, ));

        //array contains  every year that has been checked
        $year_month_data[] .= json_decode($year_month_data[] = substr($current_year_parse[2], 0, 4, ));

    // calculate every single month for current month
    for ($i = 0; $i < count($q_res_sales_per_month); $i++) {

        //parse data
        $single = implode("-", $q_res_sales_per_month[$i]);
        $single_month_parse = explode("-", $single);

        // check
        if(substr($single_month_parse[1], 0, 4, ) === substr($current_year_parse[2], 0, 4, )){

            if(!in_array($single_month_parse[0], $month_array[$key]['data'])){

                //load data amount in data array
                $month_array[$key]['data'][] .= $single_month_parse[0];


//$str .=  ']';

//echo $str;

//print json_encode($month_array, JSON_NUMERIC_CHECK);
$data = json_encode($month_array);


//if(!isset($_COOKIES[$cookie_name])) {
echo $data;

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon

That's not the correct PHP file. The error states line 82..

For the logic: the years in your data are known, thus you can prepopulate your array outside of the loop. This may require a different SQL and result set, but it is easily possible. Especially as the given JSON structure is simple, it should be easily possible to return the data from your database in exact that format and order. And drop all that string fiddling. It is not necessary.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes