DaveSandwich Posted October 21, 2017 Share Posted October 21, 2017 Im trying to filter and return all that matches iphone with iphone. If i type "iphone" (see typed example below) it works fine but if i put a variable it which is $variphone it dont, even though they are both the same. typed example: $filtermodel = array_filter($modelfilter, function( $data ){ return $data['0'] == "Iphone"; //case sensitive entry variable example $filtermodel = array_filter($modelfilter, function( $data ){ return $data['0'] == $variphone; //case sensitive entry var_dump $variphone string(5) "Iphone" var_dump - typed example: array(1) { [2]=> array(7) { [0]=> string(4) "Iphone" [1]=> string(5) "Iphone 8" [2]=> string(7) "17 - 17" [3]=> string(3) "500" [4]=> string(9) "black" [5]=> string(94) "32gb" [6]=> string(94) "32gb" } } var_dump - variable example array(0) { } Link to comment Share on other sites More sharing options...
ignace Posted October 21, 2017 Share Posted October 21, 2017 $search = function($array, $input) { return array_filter($search, function($phone) use ($input) { return $phone[0] == $input ? true : false; }); }; $result = $search($modelfilter, $variphone); var_dump($result); Link to comment Share on other sites More sharing options...
kicken Posted October 21, 2017 Share Posted October 21, 2017 variable example $filtermodel = array_filter($modelfilter, function( $data ){ return $data['0'] == $variphone; //case sensitive entry You're not showing $variphone being defined there anywhere. If it's not defined within your function then it won't exist. PHP doesn't inherit variables from outer scopes automatically like some other languages. You can import variables you need using a use statement, as demonstrated in ignace's example. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.