?
Solved

PHP array_filter with argument to callback

Posted on 2014-07-29
4
Medium Priority
?
288 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 111

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 111

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

771 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