Jump to content

Archived

This topic is now archived and is closed to further replies.

johnny5

removing empty elements from the end of an array

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Take a look at the [url=http://www.php.net/array_pop]array_pop()[/url] function.

Ken

Share this post


Link to post
Share on other sites
hi

i know this method is a pain in the... but heres what i do to get rid of all empty elements

firstly 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

Share this post


Link to post
Share on other sites
[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 ;)

Regards
Huggie

Share this post


Link to post
Share on other sites
[code=php:0]
$old_array = array ( '', 'happy', 'sad', 'mad', '' );

$new_array = array_diff ( $old_array, array ( '' ) );

print_r ( $new_array );
[/code]

me!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.