重构 PHP8 中的 array_key_exists

huangapple go评论64阅读模式
英文:

Refactor array_key_exists in PHP8

问题

I just attempted to upgrade from PHP 7.4.33 to 8.0.27 and encountered the following error on a key page:

Got error 'PHP message: PHP Warning: Undefined array key 1981 in
drivers_standing_table.php on line 56

PHP message: PHP Fatal error: Uncaught TypeError: array_key_exists():
Argument #2 ($array) must be of type array, null given in
drivers_standing_table.php:56

1981 is an id (int) associated with a record in the database. For example, in this case, a competitor has an id of 1981.

Line 56 of drivers_standing_table.php (and slightly thereafter) looks like this:

// LINE 56:
if (array_key_exists($round, $shared_drivers[$driver_id])) {

  $lender_id = $shared_drivers[$driver_id][$round];
  $lender_race_result = $drivers_race_result_array[$lender_id][$round][0];
  $cls_name = $drivers_race_result_array[$lender_id][$round][1];
  $race_result = $drivers_race_result_array[$driver_id][$round][0];

  if (array_key_exists($round, $drivers_race_result_array[$driver_id])) {     ?>
    <td align='center' style="background: orangered;">
      <?php echo $race_result . '/' . $lender_race_result; ?>
    </td>
  <?php
    $exit_flag = true;
    $i++;
  } else {
  ?>
    <td align='center' class='<?php echo $cls_name; ?>'>
      <?php echo $lender_race_result; ?>
    </td>
  <?php }
}

The error is preventing the script from continuing to run, breaking the page.

To make this code compatible with PHP8, you can add checks to ensure that the variables you are accessing are of type array. You can use the isset() function to do this. Here's the updated code for line 56:

// LINE 56:
if (isset($shared_drivers[$driver_id]) && is_array($shared_drivers[$driver_id]) && array_key_exists($round, $shared_drivers[$driver_id])) {

  $lender_id = $shared_drivers[$driver_id][$round];
  $lender_race_result = $drivers_race_result_array[$lender_id][$round][0];
  $cls_name = $drivers_race_result_array[$lender_id][$round][1];
  $race_result = $drivers_race_result_array[$driver_id][$round][0];

  if (isset($drivers_race_result_array[$driver_id]) && is_array($drivers_race_result_array[$driver_id]) && array_key_exists($round, $drivers_race_result_array[$driver_id])) {     ?>
    <td align='center' style="background: orangered;">
      <?php echo $race_result . '/' . $lender_race_result; ?>
    </td>
  <?php
    $exit_flag = true;
    $i++;
  } else {
  ?>
    <td align='center' class='<?php echo $cls_name; ?>'>
      <?php echo $lender_race_result; ?>
    </td>
  <?php }
}

This should help resolve the issue and make the code compatible with PHP8.

英文:

Having just attempted to upgrade from PHP 7.4.33 to 8.0.27 I have encountered the following error on a key page:

> Got error 'PHP message: PHP Warning: Undefined array key 1981 in
> drivers_standing_table.php on line 56
>
> PHP message: PHP Fatal error: Uncaught TypeError: array_key_exists():
> Argument #2 ($array) must be of type array, null given in
> drivers_standing_table.php:56

1981 is an id (int) associated with a record in the database. For example, in this case a competitor has an id of 1981.

Line 56 of drivers_standing_table.php (and slightly thereafter) looks like this:

// LINE 56:
if (array_key_exists($round, $shared_drivers[$driver_id])) {

  $lender_id = $shared_drivers[$driver_id][$round];
  $lender_race_result = $drivers_race_result_array[$lender_id][$round][0];
  $cls_name = $drivers_race_result_array[$lender_id][$round][1];
  $race_result = $drivers_race_result_array[$driver_id][$round][0];

  if (array_key_exists($round, $drivers_race_result_array[$driver_id])) {     ?&gt;
	&lt;td align=&#39;center&#39; style=&quot;background: orangered;&quot;&gt;
		&lt;?php echo $race_result . &#39;/&#39; . $lender_race_result; ?&gt;
	&lt;/td&gt;
  &lt;?php
	$exit_flag = true;
	$i++;
  } else {
  ?&gt;
	&lt;td align=&#39;center&#39; class=&#39;&lt;?php echo $cls_name; ?&gt;&#39;&gt;
		&lt;?php echo $lender_race_result; ?&gt;
	&lt;/td&gt;
  &lt;?php }
}

The error is preventing the script from continuing to run, breaking the page.

How should I refactor line 56 to work with PHP8?

答案1

得分: 2

你可以使用 isset() 替代,因为它不会在任何嵌套级别抛出错误:

if (isset($shared_drivers[$driver_id][$round])) {

请注意,如果值为 nullisset() 将返回 false,但我认为在你的情况下这不是一个问题。

英文:

You can use isset() instead, as it won't throw an error at any nesting level:

if (isset($shared_drivers[$driver_id][$round])) {

Keep in mind that isset() will return false if the value is null though, but I don't think it's a concern in your case.

答案2

得分: 0

[array_key_exists] 期望第二个参数是一个数组,而您传递的是 null

第一个解决方案是检查第二个参数是否是一个数组,您可以使用 is_array 来实现:

if (isset($shared_drivers[$driver_id]) 
&& is_array($shared_drivers[$driver_id]) 
&& array_key_exists($round, $shared_drivers[$driver_id])) {

或者您可以使用 Null 合并运算符 来将该 null 替换为空数组:

if (array_key_exists($round, $shared_drivers[$driver_id]??[])) {
英文:

array_key_exists expect second parameter to be an array you are passing null

First solution is to check if the second parameter is an array you can do that with is_array like that :

if (isset($shared_drivers[$driver_id]) 
&amp;&amp; is_array($shared_drivers[$driver_id]) 
&amp;&amp; array_key_exists($round, $shared_drivers[$driver_id])) {

Or you can use Null Coalescing Operator to change that null with empty array

if (array_key_exists($round, $shared_drivers[$driver_id]??[])) {

答案3

得分: 0

你可以先检查外部数组键是否存在,以及该值是否为数组,通过 if 语句逐层进入数组级别:

if ( is_array($shared_drivers)
     && array_key_exists($driver_id, $shared_drivers) 
     && is_array($shared_drivers[$driver_id]) 
     && array_key_exists($round, $shared_drivers[$driver_id]) ){

这意味着如果 $shared_drivers[$driver_id] 不存在或不是数组,IF 语句会在测试无效的数组引用($shared_drivers[$driver_id])之前中止。

如果你确定 $shared_drivers 此时是一个存在的数组,你可以通过移除第一个参数来简化上述代码。

英文:

You can check that the outer array key exists first and that this value is an array, stepping into the array levels through the if statement:

if ( is_array($shared_drivers)
     &amp;&amp; array_key_exists($driver_id, $shared_drivers) 
     &amp;&amp; is_array($shared_drivers[$driver_id]) 
     &amp;&amp; array_key_exists($round, $shared_drivers[$driver_id]) ){

This means that if the $shared_drivers[$driver_id] doesn't exist or is not an array the IF statement will abort before testing an invalid array reference ( $shared_drivers[$driver_id]) .

If you're certain that $shared_drivers is an array and exist at this point, you can streamline the above code by removing the first argument.

huangapple
  • 本文由 发表于 2023年2月8日 22:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定