mtylerb Posted October 29, 2008 Share Posted October 29, 2008 Is there something that is more efficient than array_pop? I'm currently using it like: <?php private function trimc($limit) { while (count($this->children) != $limit) { array_pop($this->children); } } ?> The code is used to trim the end results off an array which is the result of multiple arrays being combined together and sorted. My method seems rather inefficient and was the only way that I could think of doing it. The array that I'm wanting to trim is an array of PageArchive Objects. Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/ Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 By using array_pop(), you're actually removing it from the array...I'd suggest array_map() or a foreach { } with a counter. Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677919 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 if you really do want to remove elements from the array, probably array_slice(): http://il2.php.net/manual/en/function.array-slice.php Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677922 Share on other sites More sharing options...
mtylerb Posted October 29, 2008 Author Share Posted October 29, 2008 @bobbinspro - Yes, I do want to remove the end objects completely from the array depending on the limit given. I'll take a look at that function. Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677926 Share on other sites More sharing options...
mtylerb Posted October 29, 2008 Author Share Posted October 29, 2008 So, something like: <?php private function trimc($limit) { array_slice($this->children, 0, $limit); } ?> Would be more efficient? Does this automatically rewrite the array or do I need to save it back into the variable? Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677933 Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 It automatically resets the array indexes, yeah. Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677939 Share on other sites More sharing options...
mtylerb Posted October 29, 2008 Author Share Posted October 29, 2008 Ok, it doesn't actually automatically update the array, I had to do: <?php private function trimc($limit) { $this->children = array_slice($this->children, 0, $limit); } ?> But it works now, thanks (again) guys! Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677947 Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 Yeah, I meant that it automatically reassigns the keys, but you have to actually give it to a variable. Link to comment https://forums.phpfreaks.com/topic/130647-solved-array_pop/#findComment-677951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.