Michdd Posted September 16, 2009 Share Posted September 16, 2009 It's a really simple question, I browsed the manual and couldn't find something to what I wanted. Basically I have an array of objects that'll be added to and removed from often. Every time an object is removed I call array_values(&$array) so it'll keep the lowest possible indexes. However when I use $array[] = 'something'; the index of that element it puts in isn't the lowest possible. Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/ Share on other sites More sharing options...
Garethp Posted September 16, 2009 Share Posted September 16, 2009 This inserts the item at the begining of the array (IE, index 0) http://au.php.net/manual/en/function.array-unshift.php Is that what you want? Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919783 Share on other sites More sharing options...
Michdd Posted September 16, 2009 Author Share Posted September 16, 2009 Nope, that's not it. Let me try to explain it a bit better. Here's an example: $array = Array("something", "something"); //Keys would be 0, 1 unset($array[0]); //Keys: 1 array_values(&$array); //Keys are now 0 $array[] = "something"; //Keys are 0, 2 I want it to be 0, 1. Not 0, 2 Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919786 Share on other sites More sharing options...
Garethp Posted September 16, 2009 Share Posted September 16, 2009 function pushtoend($Array, $Value) { $i = -1; foreach($Array as $k=>$v) { $j = $i+1; if($k != $j) { $Array[$j] = $Value; return $Array; } $i = $k; } } Try that? Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919789 Share on other sites More sharing options...
Michdd Posted September 16, 2009 Author Share Posted September 16, 2009 That's pointless. I was thinking there must be a function for resetting the next index number in memory, I guess not.. Instead of using your function I'd just do like.. $array[count($array)]; Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919796 Share on other sites More sharing options...
Mark Baker Posted September 16, 2009 Share Posted September 16, 2009 $array = array('A','B','C'); unset($array[1]); $array = array_merge($array); print_r($array); But it does raise the question of why the array key values are so critical Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919799 Share on other sites More sharing options...
Michdd Posted September 16, 2009 Author Share Posted September 16, 2009 It's not necessarily really that critical. But I have a Character class, then my Server class has an array of these Character classes. I want to keep the key of each Character instance within that array the same as it's id and keep them at their lowest possible value, just to keep it organized. Link to comment https://forums.phpfreaks.com/topic/174516-solved-is-there-a-function-for-this/#findComment-919800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.