skip to Main Content

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])) {     ?>
    <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.

How should I refactor line 56 to work with PHP8?

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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]) 
    && is_array($shared_drivers[$driver_id]) 
    && 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]??[])) {
    
    Login or Signup to reply.
  3. 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)
         && array_key_exists($driver_id, $shared_drivers) 
         && is_array($shared_drivers[$driver_id]) 
         && 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search