Reputation: 2566
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" }}
Upvotes: 2
Views: 2137
Reputation: 237845
PHP's json_encode
function will always encode PHP strings as JSON strings. If the data goes into JSON as a string, it was a string in PHP as well.
Why it is a string in PHP is an interesting question. Perhaps the data comes from a database?
In any case, the solution is to convert the string into an integer (with (int)
) when you pass it to json_encode
:
$dataArray[$key] = (int) $value;
NB that I wouldn't check with is_numeric
. The API is clearly expecting the value to be an integer, so I'd suggest you provide one. If the value can't be converted, it will be 0
, which is probably a sensible default in your case.
OK, the above code is wrong, because it attempts to convert the array into an integer. The problem, then, is how that array is constructed. It's very hard to help you fix this if you can't show the line of code that creates the value that isn't working.
But I'll bet £5 that the solution is (int)
.
Per the latest update. I hadn't realised the data was coming from Google Analytics. You get the data and it looks like this: [1]=> string(4) "2582"
. That is obviously a string that contains number characters. You need to convert this to a number using (int)
. You should do this in your loop:
if($i == 3){
break;
}
$value[1] = (int) $value[1]; // cast the number to an int
$dataArray[$key] = $value;
Upvotes: 4
Reputation: 7317
Assuming you can't change the data at its source (you say it is coming from an external API), you may have to explicitly convert the string to an integer yourself:
$dataArray[$key] = intval($value, 10);
or
$dataArray[$key] = (int)$value;
You may want to check that $value is actually a valid number before converting it - use is_numeric
for that
Upvotes: 0
Reputation: 556
Try changing assignment line to this:
$dataArray[$key] = is_numeric($value) ? (int)$value : $value;
Note: it's not a generic solution, but will work in this particular case.
Upvotes: 2
Reputation: 1996
Use intval() (assuming they're all integers, otherwise use floatval()).
$dataArray = array();
$i = 0;
foreach ($arrSiteValue as $key => $value) {
if($i == 3){
break;
}
$dataArray[$key] = intval($value);
$i ++;
}
array_unshift($dataArray, array("Terms","Visits"));
echo json_encode($php_array);
Upvotes: 0