Results 1 to 8 of 8

Thread: [RESOLVED] Removing blank keys in array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] Removing blank keys in array

    After using array_unique() I appear to have an array that has blank fields in it.

    (1-1,1-5,,1-0,,,,1-2)

    Any Sugestions on the removal of said fields, I've tried this...
    PHP Code:

    function myfunction($v){
    if (
    $v==="")  {
      return 
    false;  }
    return 
    true;}
    array_filter($arrayday,"myfunction"); 
    ???

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Removing blank keys in array

    How about array_diff?
    http://us3.php.net/array_diff

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Removing blank keys in array

    ..? how would array_diff() do anything? that function compares multiple arrays. he only has one array.

    you should be able to use array_filter() without a callback function to remove false, empty and null values. there is an example on php.net:
    PHP Code:
    <?php

    $entry 
    = array(
                 
    => 'foo',
                 
    => false,
                 
    => -1,
                 
    => null,
                 
    => ''
              
    );

    print_r(array_filter($entry));
    ?>
    The above example will output:

    Code:
    Array
    (
        [0] => foo
        [2] => -1
    )
    keys will be preserved, though.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Removing blank keys in array

    Quote Originally Posted by kows View Post
    ..? how would array_diff() do anything? that function compares multiple arrays. he only has one array.
    I don't know. Does this work?

    $arr = array_diff($arr, array(""));

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Removing blank keys in array

    touche! I didn't even think about that. array_filter() with no callback and array_diff() with an array with an empty string should both produce the same result. though, array_filter() seems like it could be faster.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Removing blank keys in array

    Thanks I'll give them both a try

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Removing blank keys in array

    Because the $keys are no longer [1][2][3]... and have gaps in them [1][3][6]... how can I echo/print them in a list?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Removing blank keys in array

    never mind, I can do it with...
    PHP Code:
    foreach ($array as $value){
           echo 
    $value;} 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width