0

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

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;
}

我收到了这个错误:

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

我试过了:

 return substr( $el, 0, 2) == ''.(string)$this->id;'-'; 

但没有运气:(

4

1 回答 1

5

您应该能够使用闭包来完成此操作:

$slikeid = array_filter($slike, function($el) use( $id) { 
    return substr( $el, 0, 2) == $id; 
});

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

于 2012-07-31T20:03:47.527 回答