I was surprised, if I noticed it right, that $this->Js->object() autosorts the array i have given into it. Im coding using CakePHP 2.0 Here’s what i got:
In my controller:
$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'];
}
//..
In my View file:
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']);
}
}
I debugged first the value of $arrayOfPlotPoints and copied the values as it is. And I tried alerting the values that are being fetched in the for loop of my javascript. Their occurence are not alike with what i have debugged previously in my controller. It is as if it has been sorted according to value. Does this mean that JS helper autosorts its content? If that is the case, can I disable the autosort?
Sorry my mistake. Thanks to the very keen eyes of this friend of mine, he found my problem.
My array looks something like this:
and so on.. what my code above was it iterates thru each key in my inner array thus duplicating the values i push in my javascript array depending on how many elements i fetched per loop. I hope i made myself clear there 🙂 … Anyway, here’s what I got now..