Note that there are some explanatory texts on larger screens.

plurals
  1. POValue in array is a string not an integer
    primarykey
    data
    text
    <p>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 <code>json_encode</code>. I then needed to add headers to the array so I used <code>array_unshift()</code>. </p> <p>Here is the script</p> <pre><code> $dataArray = array(); $i = 0; foreach ($arrSiteValue as $key =&gt; $value) { if($i == 3){ break; } $dataArray[$key] = $value; $i ++; } array_unshift($dataArray, array("Terms","Visits")); echo json_encode($php_array); </code></pre> <p>Here is what is returned:</p> <p><img src="https://i.stack.imgur.com/60b84.png" alt="Console Output"></p> <p><em>Note: Where it says (not set) and (not provided), They are correct values for the string and need to be that.</em></p> <p>The value of the key should be an integer however it is getting passed back as a string</p> <p><strong>How can I get it so that the value is added as an integer?</strong></p> <p>Here is a dummy array (that works) to show what format the array should be outputted like:</p> <pre><code> $php_array = array( array('Terms', 'Visits'), array('test', 90), array('joke', 90), array('funny', 11) ); </code></pre> <p>And this is what this dummy array ouputs (which is how I need it): <img src="https://i.stack.imgur.com/y3c0a.png" alt="Console.Log output"></p> <p>*When I change the line to include (int) as recommenced by some of the users here I get this result: <img src="https://i.stack.imgur.com/RRJ6g.png" alt="New Console.log"></p> <p><strong>Edit</strong></p> <p>These are the lines that get the data from the Google Analytics Library: </p> <pre><code>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' =&gt; $dimensions, 'filters' =&gt; $filters); $arrSiteResults = $service-&gt;data_ga-&gt;get($ids,$startDate,$endDate,$metrics,$optParams); $notSetVisits = $arrSiteResults['rows'][0][1]; // Visits and Page Views $metrics = "ga:visits"; $dimensions = "ga:keyword"; $optParams = array('dimensions' =&gt; $dimensions, 'sort' =&gt; '-ga:visits', 'max-results' =&gt; '12'); $arrSiteResults = $service-&gt;data_ga-&gt;get($ids,$startDate,$endDate,$metrics,$optParams); $arrSiteValue = $arrSiteResults['rows']; </code></pre> <p>Here is var_dump of: $arrSiteValue:</p> <pre><code>array(12) { [0]=&gt; array(2) { [0]=&gt; string(9) "(not set)" [1]=&gt; string(4) "2582" } [1]=&gt; array(2) { [0]=&gt; string(14) "(not provided)" [1]=&gt; string(4) "1504" } [2]=&gt; array(2) { [0]=&gt; string(10) "compass fm" [1]=&gt; string(3) "149" } [3]=&gt; array(2) { [0]=&gt; string(18) "compass fm grimsby" [1]=&gt; string(2) "25" } [4]=&gt; array(2) { [0]=&gt; string(9) "compassfm" [1]=&gt; string(2) "10" } [5]=&gt; array(2) { [0]=&gt; string(15) "compassfm.co.uk" [1]=&gt; string(1) "9" } [6]=&gt; array(2) { [0]=&gt; string(16) "compass fm radio" [1]=&gt; string(1) "8" } [7]=&gt; array(2) { [0]=&gt; string(18) "grimsby rugby club" [1]=&gt; string(1) "8" } [8]=&gt; array(2) { [0]=&gt; string(13) "compass radio" [1]=&gt; string(1) "7" } [9]=&gt; array(2) { [0]=&gt; string(21) "compass radio grimsby" [1]=&gt; string(1) "5" } [10]=&gt; array(2) { [0]=&gt; string(19) "www.compassfm.co.uk" [1]=&gt; string(1) "5" } [11]=&gt; array(2) { [0]=&gt; string(15) "compass fm news" [1]=&gt; string(1) "4" }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload