Solved

PHP array_filter with argument to callback

Posted on 2014-07-29
4
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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