I have a multi-dimensional array which I need to pass to Google Charts. The script is called via Ajax, runs three times and then passes back the array encoded with json_encode
. I then needed to add headers to the array so I used array_unshift()
.
Here is the script
$dataArray = array();
$i = 0;
foreach ($arrSiteValue as $key => $value) {
if($i == 3){
break;
}
$dataArray[$key] = $value;
$i ++;
}
array_unshift($dataArray, array("Terms","Visits"));
echo json_encode($php_array);
Here is what is returned:
Note: Where it says (not set) and (not provided), They are correct values for the string and need to be that.
The value of the key should be an integer however it is getting passed back as a string
How can I get it so that the value is added as an integer?
Here is a dummy array (that works) to show what format the array should be outputted like:
$php_array = array(
array('Terms', 'Visits'),
array('test', 90),
array('joke', 90),
array('funny', 11)
);
And this is what this dummy array ouputs (which is how I need it):
*When I change the line to include (int) as recommenced by some of the users here I get this result:
Edit
These are the lines that get the data from the Google Analytics Library:
if (!$_GET["rangeStartDate"]) {
$startDate = date("Y-m-d", mktime(0, 0, 0, date("m")-1, date("d")-1, date("Y")));
$endDate = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")-1, date("Y")));
} else {
$startDate = $_GET["rangeStartDate"];
$endDate = $_GET["rangeEndDate"];
}
$metrics = "ga:visits";
$dimensions = "ga:keyword";
$filters = "ga:keyword==(not set)";
$optParams = array('dimensions' => $dimensions, 'filters' => $filters);
$arrSiteResults = $service->data_ga->get($ids,$startDate,$endDate,$metrics,$optParams);
$notSetVisits = $arrSiteResults['rows'][0][1];
// Visits and Page Views
$metrics = "ga:visits";
$dimensions = "ga:keyword";
$optParams = array('dimensions' => $dimensions, 'sort' => '-ga:visits', 'max-results' => '12');
$arrSiteResults = $service->data_ga->get($ids,$startDate,$endDate,$metrics,$optParams);
$arrSiteValue = $arrSiteResults['rows'];
Here is var_dump of: $arrSiteValue:
array(12) { [0]=> array(2) { [0]=> string(9) "(not set)" [1]=> string(4) "2582" } [1]=> array(2) { [0]=> string(14) "(not provided)" [1]=> string(4) "1504" } [2]=> array(2) { [0]=> string(10) "compass fm" [1]=> string(3) "149" } [3]=> array(2) { [0]=> string(18) "compass fm grimsby" [1]=> string(2) "25" } [4]=> array(2) { [0]=> string(9) "compassfm" [1]=> string(2) "10" } [5]=> array(2) { [0]=> string(15) "compassfm.co.uk" [1]=> string(1) "9" } [6]=> array(2) { [0]=> string(16) "compass fm radio" [1]=> string(1) "8" } [7]=> array(2) { [0]=> string(18) "grimsby rugby club" [1]=> string(1) "8" } [8]=> array(2) { [0]=> string(13) "compass radio" [1]=> string(1) "7" } [9]=> array(2) { [0]=> string(21) "compass radio grimsby" [1]=> string(1) "5" } [10]=> array(2) { [0]=> string(19) "www.compassfm.co.uk" [1]=> string(1) "5" } [11]=> array(2) { [0]=> string(15) "compass fm news" [1]=> string(1) "4" }}