使字符串对象?

Mio*_*vic 0 php string function

将字符串转换为对象时遇到问题.这是功能:

public function slikepoid($dire,$id)
{
    $this->dire=$dire;
    $this->id=$id;
    $slike = $this->skupljanjeslika($this->dire);
    $slikeid = array_filter($slike, function($el) { 
        return substr( $el, 0, 2) == '$this->id-'; // Here is the problem !
    });
    return $slikeid;
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

致命错误:在第8行的对象上下文中使用$ this

我试过了:

 return substr( $el, 0, 2) == ''.(string)$this->id;'-'; 
Run Code Online (Sandbox Code Playgroud)

但没有运气:(

nic*_*ckb 5

您应该能够使用闭包来完成此任务:

$slikeid = array_filter($slike, function($el) use( $id) { 
    return substr( $el, 0, 2) == $id; 
});
Run Code Online (Sandbox Code Playgroud)

现在,$id应该在匿名函数的范围内,因此您应该能够将元素值与它进行比较.