Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bernardev's avatar

Why its showing undefined index errors?

I have this to get some info from an API:

$postInformation = (new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
$postId =  $postInformation['id'];

Then I have this:

$data =
            array_map(fn ($category) => [
                'post_id' => $postId,
                'category_id' => $category['id'],
                'category_name' => $category['name']['en'],
            ], $postInformation['categories']['data'], ['category_id', 'category_name']);

DB::table('post_categories  ')->insertOrIgnore($data);   

But it shows an error when I refresh the page:

{message: "Undefined array key "id"", exception: "ErrorException",…} exception: "ErrorException" file: "/var/www/html/app/Nova/Post.php" line: 59 message: "Undefined array key \"id\"" trace: [{,…}, {file: "/var/www/html/app/Nova/Postl.php", line: 59,…},…]. 

If I hardcode the $postId like, " $postId = 5;" it shows:

file: "/var/www/html/app/Nova/Post.php"
line: 69
message: "Undefined array key \"categories\""
0 likes
8 replies
Nakov's avatar

Because of this: $postId = $postInformation['id'];

so you are trying to get $postInformation['id'] which does not exists there.

Try this dd($postInformation); and make sure that your array data contains all the data you need.

1 like
bernardev's avatar

@Nakov Thanks. The thing is that it seems that those indexes exist, that dump shows:

^ array:38 [
  "id" => 5
  .....
  "categories" => array:2 [
    "data" => array:15 [
      0 => array:3 [
        "id" => 6
        "name" => array:1 [
          "en_gb" => "General"
        ]
        "on_request" => 0
      ]
      1 => array:3 [
        "id" => 14
        "name" => array:1 [
          "en_gb" => "Tuts"
        ]
        "on_request" => 0
      ]
  ]
 
	....
  ]
]
Nakov's avatar

@bernardev not all though.. You are trying this:

'category_name' => $category['name']['en'],

and the key is en_gb. The error is really self descriptive, I don't know what kind of data you have, and if this dump is the only thing that you get. So make sure that you either add defaults, and then check using isset() if the key exists in the array, or a default like this

'category_name' => $category['name'] ?? 'Category name does not exists',

and you'll see which data does not work.

1 like
bernardev's avatar

@Nakov Thanks. But it seems really strange. For example

$postInformation = (new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]); $postId = $postInformation['id'];

dd($postId), this shows 5 on the network tab.

But then when its used here:

$data = array_map(fn ($category) => [ 'post_id' => $postId,

It shows the undefined index error.

Nakov's avatar

@bernardev

the issue is not when you use $postId, but it is when you are trying to access something from an array which does not exists. Read the message:

message: "Undefined array key "categories""

means that when you try $postInformation['categories'] it throws an error because the key categories does not exists on that array.

So again, the message of the error contains the solution of your problem, just read it, and make sure that you either check if that key exists before you use it and provide a default message, or just change the way you are using it. That's all.

bernardev's avatar

@Nakov Thanks, but it's strange it works with data_get(), $postId = data_get($postInformation, 'id');. The index seems that exist.

But now it shows: {message: "array_map(): Argument #2 ($array) must be of type array, null given",…} exception: "TypeError" file: "/var/www/html/app/Nova/Post.php" line: 69 message: "array_map(): Argument #2 ($array) must be of type array, null given".

If I use like this instead:

$data = array_map(function ($category) use ($postId) { return [ ... ]; }, $postInformation['categories']['data'], ['category_id', 'category_name'])

it shows: message: "Undefined array key "categories"".

Nakov's avatar

@bernardev okay, good luck. If you don't have time to read what I am saying, or read the error message, I certainly don't have time to teach you PHP.

bernardev's avatar

@Nakov Thanks, I read what you said but not understanding, when you say "the key categories does not exists on that array.", the key exists so I'm not understanding what's the issue. On the dump it shows the index and with data_get() it works.

Please or to participate in this conversation.