ホーム > Laravel > How to resolve "array_key_exists() expects parameter 2 to be array, int given" in Laravel Redis
Laravel

How to resolve "array_key_exists() expects parameter 2 to be array, int given" in Laravel Redis

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

When configuring Laravel Redis,

An error occurred: "array_key_exists() expects parameter 2 to be array, int given"

I have summarized the solution for when this error occurs.

Environment Verified

PHP 7.4
Laravel 6
Predis 2.2
Cluster mode

Conclusion

The configuration in config/database.php is incorrect.

A number is present where an array is expected.

'redis' => [
    'client' => env('REDIS_CLIENT', 'predis'),
        'options' => [
          'cluster' => env('REDIS_CLUSTER', 'redis'),
        ],
        'clusters' => [
            'default' => [
                [
                    'host' => env('REDIS_HOST', 'localhost'),
                    'password' => env('REDIS_PASSWORD', null),
                    'port' => env('REDIS_PORT', 6379),
                    'database' => 0,
                    'read_write_timeout' => 0,
                ],
            ],
        ],
    ],

The 'default' part was incorrect as follows.

// Example of incorrect configuration
'default' => [
  'host' => env('REDIS_HOST', 'localhost'),
  'password' => env('REDIS_PASSWORD', null),
  'port' => env('REDIS_PORT', 6379),
  'database' => 0,
  'read_write_timeout' => 0,
],

Structure of 'default' differs between cluster mode and normal mode

Normal Configuration

'default' => [
    'host' => env('REDIS_HOST', 'localhost'),
    'password' => env('REDIS_PASSWORD', null),
    'port' => env('REDIS_PORT', 6379),
    'database' => 0,
    'read_write_timeout' => 60,
],

Cluster Configuration

  'default' => [
      [
          'host' => env('REDIS_HOST', 'localhost'),
          'password' => env('REDIS_PASSWORD', null),
          'port' => env('REDIS_PORT', 6379),
          'database' => 0,
      ],
  ],

It is easier to notice the difference when looking at this part alone, but it can be difficult to notice in the midst of other configurations.

By the way, I encountered this issue because I mistakenly copied the configuration of the normal mode default when switching to cluster mode.

Summary

That's all for now. I hope this can be helpful to someone.

This blog is supported by clicks on advertisements. Thank you.

Goodbye!

Popular Articles

Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2

Implementing Breadcrumbs in Laravel with laravel-breadcrumbs

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!