The Little Guy Posted March 7, 2008 Share Posted March 7, 2008 I have an array that could look simular to this: playersArr = Array(1 => 'cat', 3 => 'fish', 9 => 'bird', 5 => 'mouse'); function removePlayer(id){ playersArr.splice(id,1); } Then there is a function, that is supposed to remove an element from the array (removePlayer). I then have a button next to each one to remove it from the array. So if you want to remove one you press the button and it fires up removePlayer() and removes that id from the array. The problem is that if I remove from the array 1, 3, 9, 5 in that order (one at a time) it will remove all but #5. But... If I remove from the array 5, 9, 3, 1 in that order (one at a time) it will remove them all. does that make any sense??? Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/ Share on other sites More sharing options...
fenway Posted March 7, 2008 Share Posted March 7, 2008 What's wrong with "delete"? Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-486225 Share on other sites More sharing options...
The Little Guy Posted March 7, 2008 Author Share Posted March 7, 2008 Thank You! Never though of that... Not even sure I knew there was a delete Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-486231 Share on other sites More sharing options...
The Little Guy Posted March 7, 2008 Author Share Posted March 7, 2008 If I have 1 key value pair, and I delete it. why does it still think it is in there? if I alert(playersArr.length); it says 2 (1 key 1 value = 2) Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-486253 Share on other sites More sharing options...
fenway Posted March 7, 2008 Share Posted March 7, 2008 That's quirky because you're using integers as hash keys... associative array in JS are funny. Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-486294 Share on other sites More sharing options...
The Little Guy Posted March 9, 2008 Author Share Posted March 9, 2008 Any Ideas? Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-487488 Share on other sites More sharing options...
fenway Posted March 10, 2008 Share Posted March 10, 2008 Sure... don't use integers, or prefix them with underscores. Link to comment https://forums.phpfreaks.com/topic/94920-remove-from-associative-array/#findComment-488472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.