Solved

PHP array_filter with argument to callback

Posted on 2014-07-29
4
278 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

773 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