Reputation: 962
I have a PHP array, that I want to pass over to jQuery and update the rows on the page.
This PHP array is the 'name' of the checkboxes selected on the page. (So this array can be of any length, depending on what the user selects)
PHP Array:
var_dump($sr->conflict_return);
OUTPUT CHECK: array(5) { [0]=> string(33) "hours_9_7_reg_session_102_905_925" [1]=> string(33) "hours_9_7_reg_session_101_905_925" [2]=> string(33) "hours_9_7_reg_session_103_905_925" [4]=> string(33) "hours_9_7_reg_session_104_845_915" [13]=> string(33) "hours_9_7_reg_session_103_845_905" }
This this case... there are '5' elements in my [php] array.
Here is where my problem comes into play...
Sometimes it 'works'... sometimes it doesnt..
The 'key' seems to be what is IN the array:
OUTPUT CHECK: array(3) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_104_845_915" }
this seems to work.. 3 items in array.. all 3 rows on stage get highlighted.
this:
OUTPUT CHECK: array(4) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_103_845_905" [4]=> string(33) "hours_9_7_reg_session_104_845_915" }
doesnt work... and none of the rows are highlighted
(seems like if there is 4 items in the array it breaks??)
my jQuery to parse the data:
var conflictItems = <?=json_encode($sr->conflict_return); ?>;
//has a conflict list
if(conflictItems.length > 0){
alert("Has conflicts");
//loop through and highlight elements on stage
for(i=0; i<conflictItems.length; i++){
console.log(conflictItems[i]);
$("#sr_table_"+conflictItems[i]+"_row").addClass("conflict_border");
}
}
When I trace (console.log()) the data.... I get odd results.
console.log('CONFLICT ITEMS: ' + conflictItems);
console.log('CONFLICT COUNT: ' + conflictItems.length);
3 x items in array... the above shows:
CONFLICT ITEMS: hours_9_7_reg_session_103_845_905,hours_9_7_reg_session_102_845_905,hours_9_7_reg_session_104_845_915
CONFLICT COUNT: 3
which to me is correct. I have 3 items in my array passed over from PHP..jQuery runs through list and adds a class to each 'row'.
however, when I add a 4th item... the traced output is:
CONFLICT ITEMS: [object Object]
CONFLICT COUNT: undefined
So how? is my array turning into an object?.. and more so WHY??
and how can I fix this? I dont understand why having 3 items in the array works.. but not 4?
Upvotes: 0
Views: 97
Reputation: 4187
Javascript doesn't have a concept of non-sequential array keys (your example has keys 0,1,2 and 4), thus when running json_encode
on the array, it converts it to JSON notation for a JS object.
As @Kenney says in the comments, a possible solution is to keep using arrays, but make use of array_values()
function which takes your array, and basically regenerates the array dropping the existing keys and using sequential ones.
Upvotes: 3
Reputation: 33
You're missing index 3 from your array, therefore js turns it into an object.
Upvotes: 0