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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 It automatically resets the array indexes, yeah. Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.