johnny5 Posted September 28, 2006 Share Posted September 28, 2006 Can anybody tell me hopw to remove just the empty elements on the end of array. I'm saving the array as a serialized TEXT in mysql and just want to drop redundent empty cells at the end.need input :) Quote Link to comment https://forums.phpfreaks.com/topic/22434-removing-empty-elements-from-the-end-of-an-array/ Share on other sites More sharing options...
kenrbnsn Posted September 29, 2006 Share Posted September 29, 2006 Take a look at the [url=http://www.php.net/array_pop]array_pop()[/url] function.Ken Quote Link to comment https://forums.phpfreaks.com/topic/22434-removing-empty-elements-from-the-end-of-an-array/#findComment-100597 Share on other sites More sharing options...
thedarkwinter Posted September 29, 2006 Share Posted September 29, 2006 hii know this method is a pain in the... but heres what i do to get rid of all empty elementsfirstly when i create the array, i create the first element as empty, so if its in an explode for example i would say$myarray = explode("\n", "\n" . $mystring);... and then array_unique to remove duplicates of empties$myarray = array_unique($myarray);... and then slice of the first elemnt$myarray = array_slice($myarray,1);This only works is you know that all the elements of the array are going to be unique, so it might not be best for you... hope its helpful Quote Link to comment https://forums.phpfreaks.com/topic/22434-removing-empty-elements-from-the-end-of-an-array/#findComment-100741 Share on other sites More sharing options...
HuggieBear Posted September 29, 2006 Share Posted September 29, 2006 [quote]Can anybody tell me how to remove just the empty elements on the end of array.[/quote]Maybe you should be looking at why you have empty elements at the end of an array in the first place ;)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22434-removing-empty-elements-from-the-end-of-an-array/#findComment-100746 Share on other sites More sharing options...
printf Posted September 29, 2006 Share Posted September 29, 2006 [code=php:0]$old_array = array ( '', 'happy', 'sad', 'mad', '' );$new_array = array_diff ( $old_array, array ( '' ) );print_r ( $new_array );[/code]me! Quote Link to comment https://forums.phpfreaks.com/topic/22434-removing-empty-elements-from-the-end-of-an-array/#findComment-100751 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.