52°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • Filtering Array Elements in PHP Using an Anonymous Function

    February 9, 2009

    UPDATE

    This is now basically obsolete thanks to PHP 5.3's closures

    I've used PHP's array_filter() in the past to filter elements from an array, but occasionally I find that I need to pass an additional value to the function in order to determine whether to keep the element. This just came up in a case where I was using GET variables to filter search results on a large array.

    I have a large 2D array of books where each record has some information such as title, author, publisher, ISBN, etc. I include search boxes for each column when I draw the table to the screen, so the user can search a specific column for some text.

    <?php
    $data = array(
        0 => array('Title' => "Career and Life Planning",  'Author' => "Michel Ozzi",      'ISBN' => "978-0-07-284262-3"),
        1 => array('Title' => "Beginning Algebra",         'Author' => "Blitzer",          'ISBN' => "978-0-555-03971-7"),
        2 => array('Title' => "Primary Flight Briefing",   'Author' => "Federal Aviation", 'ISBN' => "978-1-5602755-7-2")
    );
    
    foreach( $_GET as $k=>$v )
    {
        $data = array_filter($data, create_function('$data', 'return ( stripos($data["'.$k.'"], "'.$v.'") !== false );'));
    }
    
    ?>
    

    Notice the values of $k and $v are actually written into the PHP code, whereas $data is written to the function as a variable name. If you were to echo the body of the function, it would actually look something like this:

    return ( stripos($data["ISBN"], "555") !== false );
    

    In each iteration of the loop the function is re-created, and the key/value pair are essentially hard-coded into the function instead of being variables.

    So there you go! A real-live use for anonymous functions in PHP!

    Mon, Feb 9, 2009 10:35am -08:00 #anonymous functions #array #filter #PHP #search
Posted in /articles

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • 🎥 YouTube Tutorials and Reviews
  • 🏠 We're building a triplex!
  • ⭐️ Life Stack
  • ⚙️ Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv