如何修复:警告:isset 中的非法偏移类型或 F:\xampp\htdocs\digikalamvc\core\model.php 中的非法偏移类型(第 140 行)
How to fix: Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140
如何修复此错误:
Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140
Fatal error: Uncaught Error: Unsupported operand types in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php:90 Stack trace: #0 F:\xampp\htdocs\digikalamvc\controllers\showcart4.php(31): model_showcart4->saveOrder(Array) #1 F:\xampp\htdocs\digikalamvc\core\app.php(34): Showcart4->saveorder() #2 F:\xampp\htdocs\digikalamvc\index.php(7): App->__construct() #3 {main} thrown in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php on line 90
第 140 行 model.php 中的代码:( if (isset($_SESSION[$name])) {
)
public static function sessionGet($name) {
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}
第 90 行 model_showcart4.php 中的代码:
$priceTotal =$basketPrice-$basketDiscount+$postprice;
代码model_showcart4:
$basket = $basketInfo[0];
$basket = serialize($basket);
$basketPrice = $basketInfo[1];
$basketDiscount = $basketInfo[2];
$postprice = $this->calculatePostPrice($addressInfo['city']);
$postType = self::sessionGet(['postType']);
if ($postType == 1) {
$postprice = $postprice['pishtaz'];
} elseif ($postType == 2) {
$postprice = $postprice['sefareshi'];
}
$priceTotal =$basketPrice-$basketDiscount+$postprice;
代码 showcart4.php 第 31 行:
function saveorder() {
$this->model->saveOrder($_POST);
}
$postType = self::sessionGet(['postType']);
如您所见,参数是一个数组。
所以在这里
isset($_SESSION[$name])
代码变得无效,因为数组键应该是整数或字符串,而不是数组。
$postType = self::sessionGet('postType');
- 我猜这应该有效
您的警告表明为您的函数指定的键在 $_SESSION 数组中不存在。在第 83 行的 model_showcart4.php
中,您向函数传递了一个数组,这可能是导致此错误的原因。
我建议在您的 if 语句中添加额外的检查。使用 array_key_exists
函数检查键是否确实存在于给定数组中,因此如果由于某种原因给出的键根本不存在,您的代码就不会中断。
F:\xampp\htdocs\digikalamvc\core\model.php
public static function sessionGet($name) {
if (array_key_exists($name, $_SESSION) && isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}
如何修复此错误:
Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140
Fatal error: Uncaught Error: Unsupported operand types in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php:90 Stack trace: #0 F:\xampp\htdocs\digikalamvc\controllers\showcart4.php(31): model_showcart4->saveOrder(Array) #1 F:\xampp\htdocs\digikalamvc\core\app.php(34): Showcart4->saveorder() #2 F:\xampp\htdocs\digikalamvc\index.php(7): App->__construct() #3 {main} thrown in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php on line 90
第 140 行 model.php 中的代码:( if (isset($_SESSION[$name])) {
)
public static function sessionGet($name) {
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}
第 90 行 model_showcart4.php 中的代码:
$priceTotal =$basketPrice-$basketDiscount+$postprice;
代码model_showcart4:
$basket = $basketInfo[0];
$basket = serialize($basket);
$basketPrice = $basketInfo[1];
$basketDiscount = $basketInfo[2];
$postprice = $this->calculatePostPrice($addressInfo['city']);
$postType = self::sessionGet(['postType']);
if ($postType == 1) {
$postprice = $postprice['pishtaz'];
} elseif ($postType == 2) {
$postprice = $postprice['sefareshi'];
}
$priceTotal =$basketPrice-$basketDiscount+$postprice;
代码 showcart4.php 第 31 行:
function saveorder() {
$this->model->saveOrder($_POST);
}
$postType = self::sessionGet(['postType']);
如您所见,参数是一个数组。
所以在这里
isset($_SESSION[$name])
代码变得无效,因为数组键应该是整数或字符串,而不是数组。
$postType = self::sessionGet('postType');
- 我猜这应该有效
您的警告表明为您的函数指定的键在 $_SESSION 数组中不存在。在第 83 行的 model_showcart4.php
中,您向函数传递了一个数组,这可能是导致此错误的原因。
我建议在您的 if 语句中添加额外的检查。使用 array_key_exists
函数检查键是否确实存在于给定数组中,因此如果由于某种原因给出的键根本不存在,您的代码就不会中断。
F:\xampp\htdocs\digikalamvc\core\model.php
public static function sessionGet($name) {
if (array_key_exists($name, $_SESSION) && isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return false;
}
}