1

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); 
}

I've also tried using a lambda but I just can't make it work. Any ideas??

MB34
  • 4,210
  • 12
  • 59
  • 110
  • 1
    If you want to make that variable global to your `getUniv()` function, place the global statement inside the function. – showdev Jul 29 '14 at 22:22

4 Answers4

5
// even using a global variable doesn't 
// make it visible in getUniv() function
global $univ_seg;
$univ_seg = 'FSU';

That's not how globals work - to access a global variable, you need a matching global declaration in the each scope you want to use it in.

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

This would work better as an anonymous function, though:

$re = array_filter($univ_sched, function getUniv($var) use ($univ_seg) {
    return($var["EventType"] == "UN" && $var["LocationURL"] == $univ_seg);
});
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
0

Why not try it with an anonymous function instead? I find callbacks work better with that

function getUA($data, $univ_seg) {
    $univ_sched = json_decode($data, true);
    $re = array_filter($univ_sched, function($var){
        return($var["EventType"] == "UN" && $var["LocationURL"] == 'FSU');
    });
    print_r($re); 
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
0

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);
}
MB34
  • 4,210
  • 12
  • 59
  • 110
0

You could use a lambda function:

$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")
));

$univ_seg = 'FSU';

getUA($data, $univ_seg);

function getUnivFunc($univ_seg){
    return create_function('$a','return $a["EventType"] == "UN" && $a["LocationURL"] == "' . $univ_seg . '";');
}

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

http://codepad.org/LnVVry5L

Gives:

Array
(
    [0] => Array
        (
            [StartDate] => 2014/07/31
            [LocZipCode] => 19406
            [LocationURL] => FSU
            [EventType] => UN
        )

    [1] => Array
        (
            [StartDate] => 2014/08/31
            [LocZipCode] => 23513
            [LocationURL] => FSU
            [EventType] => UN
        )

)
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104