Jump to content

array_filter not === with $varible only with text entry


DaveSandwich

Recommended Posts

 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.