-2

我收到一条错误消息,指出此代码中有非法偏移:

function _generate_users($ceny, $komu, $ile, $admin=false){
    $price=($ceny*$ile);
    if($admin == false){
        $this->ceny['rent'] = $ceny['rent'];
        $this->ceny['pay'] = $ceny['pay'];
        $this->ceny['rec'] = $ceny['rec'];
    }
}//Function ends.

非法偏移在以下三行:

$this->ceny['rent'] = $ceny['rent'];
$this->ceny['pay'] = $ceny['pay'];
$this->ceny['rec'] = $ceny['rec'];
4

2 回答 2

1

您需要在引用之前检查密钥是否存在:

if ($admin == false) {
    foreach (array('rent', 'pay', 'rec') as $key) {
        if (array_key_exists($key, $ceny)) {
            $this->ceny[$key] = $ceny[$key];
        }
    }
}

但是,您也在$ceny上面使用数字类型;是什么类型的$ceny?我建议添加一个is_array检查,但看起来你需要考虑如何使用你的变量。

于 2013-10-19T11:17:25.530 回答
0

试试这个,您需要在尝试访问之前检查您的偏移量是否已经设置

private $ceny = array();

function _generate_users($ceny, $komu, $ile, $admin=false){
    $price=($ceny*$ile);
    if($admin == false){
        $this->ceny['rent'] = (isset($ceny['rent'])) ? $ceny['rent'] : '';
        $this->ceny['pay'] = (isset($ceny['pay'])) ? $ceny['pay'] : '';
        $this->ceny['rec'] = (isset($ceny['rec'])) ? $ceny['rec'] : '';
    }
}
于 2013-10-19T11:17:46.570 回答