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) { } Quote Link to comment https://forums.phpfreaks.com/topic/305420-array_filter-not-%09with-varible-only-with-text-entry/ 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); Quote Link to comment https://forums.phpfreaks.com/topic/305420-array_filter-not-%09with-varible-only-with-text-entry/#findComment-1552935 Share on other sites More sharing options...
Solution kicken Posted October 21, 2017 Solution 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. Quote Link to comment https://forums.phpfreaks.com/topic/305420-array_filter-not-%09with-varible-only-with-text-entry/#findComment-1552936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.