show me how to fix this error , although there are quite a few posts but mine is a bit different . I don't understand php well thanks ! details of the error code line: $datainfo['category'] = $category['name'];
Asked
Active
Viewed 2,477 times
-1
-
do print_r($category) and Check what result are you getting, As per the error, It's saying in $category array there is no field like "name" – KHIMAJI VALUKIYA Dec 18 '21 at 07:32
-
And For handle error, you can write code like: $datainfo['category'] = (isset($category['name']))?$category['name']:""; – KHIMAJI VALUKIYA Dec 18 '21 at 07:32
-
@KHIMAJIVALUKIYA thanks so much , it worked for me , you saved me time . – dev99 Dec 18 '21 at 07:53
2 Answers
1
For handle error, you can write code like:
$datainfo['category'] = (isset($category['name']))?$category['name']:"";

KHIMAJI VALUKIYA
- 626
- 1
- 5
- 16
1
The error states that PHP cannot find the key "name" in an array. To counteract the error, you can make the following check and assignment at the point where the array is set.
Many ways to do it:
long version
if (! isset($category['name'])) {
$category['name'] = "";
}
medium long version
$datainfo['category'] = (isset($category['name'])) ? $category['name'] : "";
shortest version
$datainfo['category'] = $category['name'] ?? "";

Maik Lowrey
- 15,957
- 6
- 40
- 79
-
-
@dev99 what kind of warning you get. i tested and it works without warning. ` ) ` – Maik Lowrey Dec 18 '21 at 08:56
-
1