简体   繁体   中英

going from PHP arrays to Javascript arrays curly braces vs square brackets

PHP Version 5.3.14

Declaring my Array and how I use it in my program: (changed into a general code scheme)

I am creating the PHP array like this:

$totalData;

getnumSends($SomeObject){

   $dataArray[0] = $someObject->Date //Date is a String
   foreach($anotherObject as $Object2){
    switch($anotherObject->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->getNumSend($someObject);  
   $someIndex+=1;

} 

does that make sense?

lets say I have a PHP array that looks like this when print_r($totalData); is called:

Array
(
    [0] => Array
        (
            [0] => 2012-04-25
            [1] => 2
            [2] => 1
            [3] => 2
            [4] => 1
            [5] => 1
            [6] => 1
        )

    [1] => Array
        (
            [0] => 2012-04-29
            [1] => 4
            [2] => 4
            [3] => 2
            [4] => 1
            [5] => 1
        )
)

now, I have some javascript code that I want to use for plotting data. the script wants data in this format:

var matrix = [[2012-04-25, 2, 1, 2, 1, 1, 1], [2012-4-29, 4, 4, 2, 1, 1]];

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:

<script>

var matrix = <?php echo json_encode($totalData); ?>;

</script>

output from "view page source":

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}]];

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.

Attacking this from the other end:

I know you said you don't want to have to manually reconstruct the array, but here's an approach to doing just that as a last resort:

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}];

var retArray = new Array();
var tmpArr1 = new Array();
var tmpArr2 = new Array();

for(var i = 0; i < retArray.length; ++i) {
    for(property in matrix[i]) {
        (i === 0) ? tmpArr1.push(matrix[i][property]) : tmpArr2.push(matrix[i][property]);
    }
}

retArray.push(tmpArr1);
retArray.push(tmpArr2);

It can likely be cleaned up/made more abstract, but that's the general approach.

One possible, yet not very "clean" approach would be to implode the arrays conveniently

function getJSArray($data){
    $jsMatrix = '[';
    foreach($data as $key => $val){
        $jsMatrix .= '[' . implode(', ', $val) . ']';
        if($key != count($data)-1)
            $jsMatrix .= ', ';
    }
    $jsMatrix .= ']';
    return $jsMatrix;
}

And then

<script>
    var matrix = <?php echo getJSArray($totalData); ?>;
</script>

With an array like

$data = array(array('2012-04-25', 2, 1, 2, 1, 1, 1),
              array('2012-04-29', 4, 4, 2, 1, 1)
             );

You would have the output

<script>
    var matrix = [[2012-04-25, 2, 1, 2, 1, 1, 1], [2012-04-29, 4, 4, 2, 1, 1]]
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM