@html_student
Молодой и глупый.

Ошибка trying to access array offset on value of type null in?

if ( is_home() ) {
			if ( $this->tree["main"]["home"]["options"] != null ) {
				$options = $this->tree["main"]["home"]["options"];
			} elseif ( $this->tree["main"]["options"] != null ) {
				$options = $this->tree["main"]["options"];
			} else {
				$options = $this->tree["all"]["options"];
			}
		}


ругается на
if ( $this->tree["main"]["home"]["options"] != null ) {

и
} elseif ( $this->tree["main"]["options"] != null ) {

Появилось после переключения на PHP 8 , я только не пойму, мне надо проверку на is_array сделать? или isset?
Получается
if ( is_home() ) {
			if (isset($this->tree["main"]["home"]["options"] )) {
				$options = $this->tree["main"]["home"]["options"];
			} elseif (isset($this->tree["main"]["options"])) {
				$options = $this->tree["main"]["options"];
			} else {
				$options = $this->tree["all"]["options"];
			}
		}

Так убираем ошибки
Но
$options = $this->tree["all"]["options"];
Тоже ругается
Trying to access array offset on value of type null
  • Вопрос задан
  • 61 просмотр
Пригласить эксперта
Ответы на вопрос 1
batyrmastyr
@batyrmastyr
Isset, причём в форме
$options = $this->tree["main"]["home"]["options"] ?? $this->tree["main"]["options"] ?? $this->tree["all"]["options"];

Но сперва прикинуть какой из уровней оказывается null вместо массива: $this->tree["main"]["home"], $this->tree["main"] или $this->tree и насколько такая ситуация нормальна?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
YCLIENTS Москва
от 200 000 до 350 000 ₽
Ведисофт Екатеринбург
от 25 000 ₽
ИТЦ Аусферр Магнитогорск
от 100 000 до 160 000 ₽