0

如果我没看错的话,我很惊讶 $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 助手会自动排序其内容?如果是这种情况,我可以禁用自动排序吗?

4

1 回答 1

0

抱歉,是我的错。多亏了我这位朋友非常敏锐的眼睛,他发现了我的问题。

我的数组看起来像这样:

Array
(
    [0] => Array
        (
            [latitude] => 14.62163
            [longitude] => 121.05123
            [signal_level] => -75
            [snr_level] => -5
            [key] => 0
            [color] => #000000
        )

    [1] => Array
        (
            [latitude] => 14.6216233333333
            [longitude] => 121.051183333333
            [signal_level] => -73
            [snr_level] => -5
            [key] => 1
            [color] => #000000
        )

    [2] => Array
        (
            [latitude] => 14.6216266666667
            [longitude] => 121.051146666667
            [signal_level] => -73
            [snr_level] => -5
            [key] => 2
            [color] => #000000
        )

    [3] => Array
        (
            [latitude] => 14.62162
            [longitude] => 121.051088333333
            [signal_level] => -75
            [snr_level] => -5
            [key] => 3
            [color] => #000000
        )

    [4] => Array
        (
            [latitude] => 14.6216383333333
            [longitude] => 121.051103333333
            [signal_level] => -75
            [snr_level] => -5
            [key] => 4
            [color] => #000000
        )

..........

等等..我上面的代码是通过我的内部数组中的每个键进行迭代,从而根据每个循环获取多少元素来复制我在我的javascript数组中推送的值。我希望我在那里表达清楚:) ...无论如何,这就是我现在得到的......

for (var key in test) {
   var obj = test[key];
// alert(test[key]['latitude']);
   latitude.push(test[key]['latitude']);
   longitude.push(test[key]['longitude']);
   color.push(test[key]['color']);
}
于 2012-07-05T09:49:36.147 回答