Jump to content


Photo

removing empty elements from the end of an array


  • Please log in to reply
4 replies to this topic

#1 johnny5

johnny5
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 September 2006 - 11:42 PM

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

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 September 2006 - 12:43 AM

Take a look at the array_pop() function.

Ken

#3 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 29 September 2006 - 09:24 AM

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


Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 09:33 AM

Can anybody tell me how to remove just the empty elements on the end of array.


Maybe you should be looking at why you have empty elements at the end of an array in the first place ;)

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 09:43 AM

$old_array = array ( '', 'happy', 'sad', 'mad', '' );

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

print_r ( $new_array );

me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users