www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 10-17-2008, 11:05 AM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    PHP trim array function

    I could have sworn there was a function to be used on arrays that would remove all empty elements. I know it isn't called trim, but I thought I have seen it before, but can't find it. Maybe I am imagining things. This is what I am looking for it to do.

    $test_array('foo', 'bar', '', 'sleep', '');

    after going through the function it the array would be

    $test_array('foo', 'bar', 'sleep');

    I know writing a function isn't that hard and I come across needing this at different times for different sites so I was just wanting to know if somebody else knows if this function exists.
    Reply With Quote
      #2  
    Old 10-17-2008, 11:11 AM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    resolved

    Found it.
    Reply With Quote
      #3  
    Old 10-17-2008, 11:13 AM
    scragar's Avatar
    scragar scragar is offline
    Some Random Guy
     
    Join Date: Jun 2003
    Location: here
    Posts: 4,477
    there isn't a built in function (check for yourself if you don't believe me: http://php.net/array ), but you can make one:
    PHP Code:
    function array_trim($ar){
      foreach(
    $ar as $key)
        if(empty(
    $ar[$key]))
          unset(
    $ar[$key]);
      return
    $ar;
    };
    which will work like:
    PHP Code:
    $test = Array('foo', 'bar', '', 'sleep', '');
    var_dump($test);
    echo
    "\n<br>\n";
    var_dump(array_trim($test));
    or you could get it to work by ref, which would be better:
    PHP Code:
    function array_trim(&$ar){
      foreach(
    $ar as $key)
        if(empty(
    $ar[$key]))
          unset(
    $ar[$key]);
    };
    Choice is yours.
    __________________
    If you are using PHP please use the [php] and [/php] forum tags for highlighting...
    The same applies to HTML and the forums [html][/html] tags.
    Reply With Quote
      #4  
    Old 10-17-2008, 11:42 AM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    Reply With Quote
      #5  
    Old 10-17-2008, 05:27 PM
    SyCo's Avatar
    SyCo SyCo is offline
    Registered User
     
    Join Date: Apr 2007
    Posts: 1,594
    Did you see the use notes on the man page?
    http://us.php.net/manual/en/function.array-filter.php

    It looks like you're using alpha values in your array but be aware it'll remove anything that factors to false or zero. If that what you want then cool.
    Reply With Quote
      #6  
    Old 10-17-2008, 07:10 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    Quote:
    Originally Posted by SyCo View Post
    Did you see the use notes on the man page?
    http://us.php.net/manual/en/function.array-filter.php

    It looks like you're using alpha values in your array but be aware it'll remove anything that factors to false or zero. If that what you want then cool.
    And if that's an issue, you can use the optional callback function parameter to tailor its functionality to exactly what you need.
    __________________
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #7  
    Old 10-17-2008, 07:23 PM
    SyCo's Avatar
    SyCo SyCo is offline
    Registered User
     
    Join Date: Apr 2007
    Posts: 1,594
    yea, just wanted to bring it to attention. It's a neat function.
    __________________
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.
    Reply With Quote
      #8  
    Old 10-17-2008, 08:54 PM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    It works like a charm. Great function.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:14 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.