HH\Vector::lastKey

Description

public function lastKey(): ?int

Returns the last key in the current Vector.

Return Values

  • ?int - The last key (an integer) in the current Vector, or null if the Vector is empty.

Examples

This example shows how lastKey() can be used even when a Vector may be empty:

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\LastKey;

function echoLastKey(Vector<string> $v): void {
  $last_key = $v->lastKey();
  if ($last_key !== null) {
    echo 'Last key: '.$last_key."\n";
  } else {
    echo 'No last key (Vector is empty)'."\n";
  }
}

// Will print "Last key: 3"
echoLastKey(Vector {'red', 'green', 'blue', 'yellow'});

// Will print "No last key (Vector is empty)"
echoLastKey(Vector {});
Output
Last key: 3
No last key (Vector is empty)