2

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:

Console Output

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): Console.Log output

*When I change the line to include (int) as recommenced by some of the users here I get this result: New Console.log

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" }}
4

4 回答 4

4

PHP 的json_encode函数将始终将 PHP 字符串编码为 JSON 字符串。如果数据作为字符串进入 JSON,那么它在 PHP 中也是一个字符串。

为什么它是 PHP 中的字符串是一个有趣的问题。也许数据来自数据库?

无论如何,解决方案是在将字符串(int)传递给时将字符串转换为整数(带)json_encode

$dataArray[$key] = (int) $value;

注意我不会检查is_numeric。API 显然期望该值是一个整数,所以我建议您提供一个。如果该值无法转换,它将是0,在您的情况下这可能是一个明智的默认值。


好的,上面的代码是错误的,因为它试图将数组转换为整数。那么,问题是如何构造该数组。如果您无法显示创建无效值的代码行,则很难帮助您解决此问题。

但我敢打赌,解决方案是 5 英镑(int)


根据最新更新。我没有意识到数据来自谷歌分析。你得到数据,它看起来像这样:[1]=> string(4) "2582". 这显然是一个包含数字字符的字符串。您需要使用 将其转换为数字(int)。你应该在你的循环中这样做:

if($i == 3){
    break;
}
$value[1] = (int) $value[1]; // cast the number to an int
$dataArray[$key] = $value;
于 2013-11-05T12:28:58.873 回答
2

尝试将分配行更改为此:

$dataArray[$key] = is_numeric($value) ? (int)$value : $value;

注意:这不是通用解决方案,但在这种特殊情况下可以使用。

于 2013-11-05T12:28:42.140 回答
0

使用intval()(假设它们都是整数,否则使用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);
于 2013-11-05T12:28:35.450 回答
0

假设您无法在其源处更改数据(您说它来自外部 API),您可能必须自己显式地将字符串转换为整数:

$dataArray[$key] = intval($value, 10);

或者

$dataArray[$key] = (int)$value;

您可能需要在转换之前检查 $value 实际上是一个有效数字 -is_numeric用于

于 2013-11-05T12:31:21.413 回答