$ this-> Js-> object()根据值自动调​​整其内容

时间:2012-07-05 06:29:32

标签: javascript cakephp helper

我很惊讶,如果我注意到它,那个$ 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帮助程序自动调整其内容?如果是这种情况,我可以禁用自动挂载吗?

1 个答案:

答案 0 :(得分: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']);
}