Professional Web Applications Themes

Removing empty array values. - PHP Development

How do I remove empty array values? -- - Zavaboy [email]zavaboyhotmail.com[/email] [url]www.zavaboy.com[/url]...

Sponsored Links
  1. #1

    Default Removing empty array values.

    How do I remove empty array values?

    --

    - Zavaboy
    [email]zavaboyhotmail.com[/email]
    [url]www.zavaboy.com[/url]


    Sponsored Links
    Zavaboy Guest

  2. #2

    Default Re: [PHP] Removing empty array values.

    Hi,

    Monday, August 18, 2003, 10:03:25 PM, you wrote:
    z> How do I remove empty array values?

    z> --

    z> - Zavaboy
    z> [email]zavaboyhotmail.com[/email]
    z> [url]www.zavaboy.com[/url]


    In a loop with unset()

    foreach($array as $key=>$val){
    if(empty($val) unset($array[$key]);
    }

    --
    regards,
    Tom

    Tom Rogers Guest

  3. #3

    Default Re: [PHP] Removing empty array values.

    On Mon, 18 Aug 2003 08:03:25 -0400, you wrote:
    >How do I remove empty array values?
    That could mean a lot of things. I'm going to assume you have a simple
    indexed array and want to remove any entries where array[index] == FALSE,
    and reindex the array.

    The most elegant way is to copy all the values you want to keep to a new
    array.

    $a = array(FALSE, 2, 3, FALSE, 5, FALSE, 7, FALSE, FALSE, FALSE);
    $b = array();
    for ($i = 0; $i < sizeof ($a); $i++)
    {
    if ($a[$i] !== FALSE)
    {
    $b[] = $a[$i];
    }
    }
    $a = $b;
    print_r ($a);

    If you don't want to reindex the array, you could use unset()

    $a = array(FALSE, 2, 3, FALSE, 5, FALSE, 7, FALSE, FALSE, FALSE);
    for ($i = 0; $i < sizeof ($a); $i++)
    {
    if ($a[$i] === FALSE)
    {
    unset ($a[$i]);
    }
    }
    print_r ($a);

    David Otton Guest

  4. #4

    Default Re: Removing empty array values.

    $entry = array(
    0 => 'foo',
    1 => false,
    2 => -1,
    3 => null,
    4 => ''
    );

    print_r(array_filter($entry));
    anil Guest

Similar Threads

  1. #38890 [NEW]: imap_sort() returns array of empty values
    By Russell dot Mosemann at cune dot edu in forum PHP Bugs
    Replies: 1
    Last Post: September 24th, 06:36 PM
  2. removing empty value
    By TESS_GEAR in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: May 19th, 02:48 PM
  3. how to check array element's values if this elementis empty
    By jiecoldfusion in forum Macromedia ColdFusion
    Replies: 0
    Last Post: February 25th, 10:03 PM
  4. [PHP] Removing empty array values.
    By Kirk Johnson in forum PHP Development
    Replies: 0
    Last Post: August 18th, 03:08 PM
  5. [PHP] Removing array element by key
    By Jason Wong in forum PHP Development
    Replies: 0
    Last Post: July 10th, 08:54 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139