[英]going from PHP arrays to Javascript arrays curly braces vs square brackets
PHP Version 5.3.14 PHP版本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: 我正在创建这样的PHP数组:
$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);
可以说我有一个PHP数组,当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. 现在,我有一些要用于绘制数据的javascript代码。 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. 为了将php数组转换为javascript数组,似乎有多种方法,但是我尝试过的每种方法都无法得到所有方括号。 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? 有没有办法从PHP 2维数组转换为Javascript 2维数组但没有花括号? 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. 我希望我只是错误地使用了JSON。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.