There is no problem not worth solving, and no technology not worth learning!

Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int give given

$bar 的值为 1 是因为 include 成功运行了。注意以上例子中的区别。第一个在被包含的文件中用了 return 而另一个没有。如果文件不能被包含,则返回 false 并发出一个 E_WARNING 警告

1. Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int give. as shown in Figure 1

在 PHP 7.4 中报错:ErrorException array_key_exists() expects parameter 2 to be array, int given
Figure 1

2. The code is implemented as follows, the reason is that require($this->getCachePath()) returns 1, and the root of the return 1 is the path $this->getCachePath() The content in the represented file is empty.

if (array_key_exists($path, $this->assetEntries)) {
    return $this->assetEntries[$path];
}
 
if (!$this->isThemeUpdated()) {
    $this->assetEntries = require($this->getCachePath());
 
    return;
}
$code = '<?php';
$code .= "\n\n";
$code .= 'return ' . var_export($assets, true) . ';';
 
$this->local->put($this->getCachePath(), $code, true);

3. In fact, when using require, it is a statement, not a function, so require($this->getCachePath()) can be replaced with require $this->getCachePath()

4. Reference:Include , the value of $BAR is 1 because the include run successfully. Notice the difference in the above example. The first one uses return in the included file and the other does not. If the file cannot be included, it returns FALSE and an e_warning warning is issued. as shown in Figure 2

$bar 的值为 1 是因为 include 成功运行了。注意以上例子中的区别。第一个在被包含的文件中用了 return 而另一个没有。如果文件不能被包含,则返回 false 并发出一个 E_WARNING 警告
Figure 2

5. Then it is speculated that the reason is that although the content in the included file is expected to be return array() , in fact, the content of the file is not the same. Because the contents of the file are written based on $this->local->put($this->getCachePath(), $code, true);

6. Simulate the actual operation of the code, require will return the case of array().

return.php

<?php
 
return [
    'a' => 1,
    'b' => 2
];
 
?>

require.php


<?php
 
var_dump(require('return.php'));
 
?>


array(2) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
}



7. Simulate the actual operation of the code, require will return the case of 1 (1), and return the number 1 in the included file.

return.php

<?php
 
return 1;
 
?>



require.php

<?php
 
var_dump(require('return.php'));
 
?>


int(1)




8. Simulate the actual operation of the code, require will return 1 (2), including the content in the php tag in the file is empty.

return.php

<?php
 
?>

require.php

<?php
 
var_dump(require('return.php'));
 
?>


int(1)




9. Simulate the actual operation of the code, require will return 1 (3), the content in the containing file is empty, and it is a blank file.

return.php



require.php

<?php
 
var_dump(require('return.php'));
 
?>


int(1)




10. In the production environment, require($this->getCachePath()) returns 1, then require will return 1 In the case (3), the content in the containing file is empty, and it is a blank file.

11. Check the put() method in illuMinate\FileSystem\FileSystem to implement

    public function put($path, $contents, $lock = false)
    {
        return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
    }


12. For specific investigation and analysis process, please refer to:File_put_contents() with empty file contents when using file_put_contents() in PHP 7.4.

Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.