Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php array_filter function not found or invalid function name regardless of what I enter

Tags:

arrays

php

I've never used the array_filter function before so giving it a go by no matter what I use as the function name it is giving me the error

Warning: array_filter() expects parameter 2 to be a valid callback, function 'odd' not found or invalid function name in

I have even taken the steps of copying paste the example directly off the php manual page and it is giving me that same error. Code:

function odd($var) {
    // returns whether the input integer is odd
    return($var & 1);
 }

function calculate($res, $period, $elements, $per, $total, $brand = false) {

    $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
    $array2 = array(6, 7, 8, 9, 10, 11, 12);

    echo "Odd :\n";
    print_r(array_filter($array1, "odd"));
}

I really don't know where to go here. Usually when I'm having trouble I can copy and paste the code exact off the php manual page and work my way back from there but if their example isn't even working it makes it hard.

like image 267
mr mojo risin Avatar asked Dec 20 '12 06:12

mr mojo risin


1 Answers

I think in your case, function odd is not a standalone function but method of your class. In that case you should write

print_r(array_filter($array1, array($this,"odd")));

to run odd method from your class

like image 132
Marcin Nabiałek Avatar answered Nov 12 '22 15:11

Marcin Nabiałek