Note that there are some explanatory texts on larger screens.

plurals
  1. POgoing from PHP arrays to Javascript arrays curly braces vs square brackets
    primarykey
    data
    text
    <p>PHP Version 5.3.14</p> <p>Declaring my Array and how I use it in my program: (changed into a general code scheme)</p> <p>I am creating the PHP array like this:</p> <pre><code>$totalData; getnumSends($SomeObject){ $dataArray[0] = $someObject-&gt;Date //Date is a String foreach($anotherObject as $Object2){ switch($anotherObject-&gt;String){ case "XX": $dataArray[1]+=1; break; case "YY": $dataArray[2]+=1; break; } } } //Method just puts an int value at certain places in the array //extra code for putting ints in the array return $dataArray; } $someIndex = 0; foreach($someObject as $Object1){ //that is very abstract $totalData[$someIndex] = $Object1-&gt;getNumSend($someObject); $someIndex+=1; } </code></pre> <p>does that make sense?</p> <p>lets say I have a PHP array that looks like this when <code>print_r($totalData);</code> is called:</p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; 2012-04-25 [1] =&gt; 2 [2] =&gt; 1 [3] =&gt; 2 [4] =&gt; 1 [5] =&gt; 1 [6] =&gt; 1 ) [1] =&gt; Array ( [0] =&gt; 2012-04-29 [1] =&gt; 4 [2] =&gt; 4 [3] =&gt; 2 [4] =&gt; 1 [5] =&gt; 1 ) ) </code></pre> <p>now, I have some javascript code that I want to use for plotting data. the script wants data in this format:</p> <pre><code>var matrix = [[2012-04-25, 2, 1, 2, 1, 1, 1], [2012-4-29, 4, 4, 2, 1, 1]]; </code></pre> <p>In order to get a php array to a javascript array there seems to be multiple ways, but every way I have tried I cannot get all square brackets. Closest I get is this: </p> <pre><code>&lt;script&gt; var matrix = &lt;?php echo json_encode($totalData); ?&gt;; &lt;/script&gt; </code></pre> <p>output from "view page source":</p> <pre><code>var matrix = [[{"0":"2012-04-25","1":2,"2":1,"3":2,"4":1,"5":1,"6":1}],[{"0":"2012-04-29","1":4,"2":4,"3":2,"4":1,"5":1}]]; </code></pre> <p>is there a way to go from the PHP 2-dimensional array to a Javascript 2-dimensional array but without curly braces? I was hoping to stay away from writing a method that fills in each spot by hand? I was hoping I was just using JSON incorrectly. Any help would be great.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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