Solved

PHP array_filter with argument to callback

Posted on 2014-07-29
4
280 Views
Last Modified: 2014-08-04
I need to filter an array in PHP but am not sure how to pass an argument to the callback. Essentially I have 2 comparisons to make on each item in the array.
// This data will be sent to the function as JSON so I'm "creating" the JSON here.
$data = json_encode(Array(
    Array("StartDate"=>"2014/07/31","LocZipCode"=>"19406","LocationURL"=>"FSU","EventType"=>"UN"),
    Array("StartDate"=>"2014/08/31","LocZipCode"=>"23513","LocationURL"=>"FSU","EventType"=>"UN"),
    Array("StartDate"=>"2014/07/31","LocZipCode"=>"92108","LocationURL"=>"BU","EventType"=>"UN"),
    Array("StartDate"=>"2014/09/30","LocZipCode"=>"78661","LocationURL"=>"BU","EventType"=>"UN")
));
                
// even using a global variable doesn't 
// make it visible in getUniv() function
global $univ_seg;
$univ_seg = 'FSU';

getUA($data, $univ_seg);

function getUniv($var){
    return($var["EventType"] == "UN" && $var["LocationURL"] == $univ_seg);
}

function getUA($data, $univ_seg) {
    $univ_sched = json_decode($data, true);
    $re = array_filter($univ_sched, "getUniv");
    print_r($re); 
}

Open in new window

I've also tried using a lambda but I just can't make it work. Any ideas??
0
Comment
Question by:EddieShipman
  • 2
  • 2
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40227963
For starters, the global declaration must be inside the function definition.  Let me see what else I can find.  It's not too hard to get this right, IIRC, but it's not something we do every day.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40227974
What is the expected output from the process above?  Are you looking for these two things to be true?

1. The LocationURL == FSU
2. The EventType == UN

If that is right, it looks like there are two data elements that match.  Would I be on firm ground if I returned an array containing those two "sub-arrays" (or more likely objects)?  Or do you need a redacted JSON string?
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 0 total points
ID: 40227995
Ray, I got it to work using an object, http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback

    class univFilter {
        public $univ_seg;
        public function filter($var) {
            if($var["EventType"] == "UN" && $var["LocationURL"] == $this->univ_seg)
                return true;
              else
                return false;
        }
    }

    $data = json_encode(Array(
        Array("StartDate"=>"2014/07/31","LocZipCode"=>"19406","LocationURL"=>"FSU","EventType"=>"UN"),
        Array("StartDate"=>"2014/08/31","LocZipCode"=>"23513","LocationURL"=>"FSU","EventType"=>"UN"),
        Array("StartDate"=>"2014/11/30","LocZipCode"=>"92108","LocationURL"=>"BU","EventType"=>"UN"),
        Array("StartDate"=>"2014/09/30","LocZipCode"=>"78661","LocationURL"=>"BU","EventType"=>"UN")
    ));
    
    $univ_seg = "BU";
    getUA($data,$univ_seg);

    function getUA($data, $univ_seg) {
        $f = new univFilter();
        $f->univ_seg = $univ_seg;
        $univ_sched = json_decode($data, true);
        $data       = array_filter($univ_sched,array($f,"filter"));
        print_r($data);
    }

Open in new window

0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 40238556
Self answered
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question