Link to home
Start Free TrialLog in
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]},
{name:"2021",
data: [809273502.95, 8792634593486.85, 893475893475.85]}
}

Open in new window


current view:
User generated image

data.php
$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);
    //name
    $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);

//setcookie($cookie_name);

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

Open in new window






Avatar of ste5an
ste5an
Flag of Germany image

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.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial