Jump to content

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


DaveSandwich
Go to solution Solved by kicken,

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

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.