Solved

PHP array_filter with argument to callback

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

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
Self answered
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now