如果我没看错的话,我很惊讶 $this->Js->object() 会自动排序我给它的数组。我使用 CakePHP 2.0 编码这是我得到的:
在我的控制器中:
$arrayOfPlotPoints = array();
//....
foreach($allFilenames as $key => $value){
$arrayOfPlotPoints[$key]['latitude'] = $value['TransactionDetail']['latitude'];
$arrayOfPlotPoints[$key]['longitude'] = $value['TransactionDetail']['longtitude'];
$arrayOfPlotPoints[$key]['signal_level'] = $value['TransactionDetail']['signal_level'];
$arrayOfPlotPoints[$key]['snr_level'] = $value['TransactionDetail']['snr_level'];
$color = $this->ColorSchema->find('first',array('fields'=>array('ColorSchema.color'),'conditions' =>array('category' => $category, 'upper_bound >=' => $value['TransactionDetail']['snr_level'], 'lower_bound <='=> $value['TransactionDetail']['snr_level'])));
$arrayOfPlotPoints[$key]['color'] = $color['ColorSchema']['color'];
}
//..
在我的视图文件中:
var test = <?php echo $this->Js->object($arrayOfPlotPoints); ?>;
for (var key in test) {
var obj = test[key];
for (var prop in obj) {
color.push(obj['color']);
latitude.push(obj['latitude']);
longitude.push(obj['longitude']);
}
}
我首先调试了 $arrayOfPlotPoints 的值并按原样复制了这些值。我尝试提醒在我的 javascript 的 for 循环中获取的值。它们的出现与我之前在控制器中调试的不同。就好像它已经根据价值进行了排序一样。这是否意味着 JS 助手会自动排序其内容?如果是这种情况,我可以禁用自动排序吗?