Support

Account

Home Forums General Issues How to fix error end() expects parameter 1 to be array?

Helping

How to fix error end() expects parameter 1 to be array?

  • $test = get_field(‘testing’, $post->ID);
    $last_row = end($test);

    causes

    PHP Warning: end() expects parameter 1 to be array, bool given in url/test.php on line 678
    PHP Notice: Trying to access array offset on value of type null in url/test.php on line 685
    PHP Notice: Trying to access array offset on value of type null in url/test.php on line 686

    How to fix that?

  • You need to test to make sure the field returns an array before using end(). If the field has no value it will return NULL.

    
    if (is_array($test)) {
      // do stuff with the array
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.