SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: array filter

Hybrid View

  1. #1
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array filter

    This function works, but feels a little messy. Is it good PHP(5)? I feel like there should be a better solution.

    PHP Code:
    function filter_array($request$filter) {
        foreach (
    $request as $key => $value) {
            if (
    in_array($key$filter)) {
                
    $result[$key] = $value;
            }
        }
        return 
    $result;
    }

    $request = array('alpha' => 'est''beta' => 'meh''gamma' => 'oui');
    $filter  = array('alpha''beta');

    $filtered filter_array($request$filter);

    // output I want:
    // $filtered == array('alpha' => 'est', 'beta' => 'meh'); 
    Thanks,
    Douglas
    Hello World

  2. #2
    SitePoint Enthusiast ssx-gun's Avatar
    Join Date
    Sep 2002
    Location
    Strongsville, OH
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why mess with something that works? but yea its really not that messy(to me) maybe there are other ways but yours seems fine.
    PHP: Pills Help People
    ---
    weird-one.com



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
  •