How to combine to array for chartjs width php

๐Ÿ‘:0

As I see the solution could be simple:

  1. Send a POST HTTP request to your PHP file.
  2. Validate you have a POST request with an if sentence and isset($_POST)
  3. Prepare or extract the data, and create an associative array like the next one:
$label_data = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
$revenue_data = [105,85,55,68,72,8,0,0,0,0,0,0];
$cost_data = [41,32,22,23,26,3,0,0,0,0,0,0];

$data = [
    'label'   => $label_data,
    'revenue' => $revenue_data,
    'cost'    => $cost_data
];

echo json_encode($data);

if you have data divided between arrays just merge them with array_merge and store the result in a new variable

$label_first = ["Jan","Feb","Mar","Apr","May","Jun"];
$label_second = ["Jul","Aug","Sep","Oct","Nov","Dec"];
$label_data = array_merge($label_first, $label_second );
  1. Finally catch the response in your $.ajax and, perform the action you want to do the data.

I hope this information could be useful for you.

Leave a comment