Skip to content

Instantly share code, notes, and snippets.

@AsaoluElijah
Last active September 26, 2020 23:36
Show Gist options
  • Save AsaoluElijah/5d9a5ac977cba2b5d471c2aeb141b556 to your computer and use it in GitHub Desktop.
Save AsaoluElijah/5d9a5ac977cba2b5d471c2aeb141b556 to your computer and use it in GitHub Desktop.
Get Last Element In An Array - PHP, JavaScript & Python

Get Last Element In An Array - PHP, JavaScript & Python

Javascript

const sampleArray = ["foo", 1, 2,"bar"];
let lastElement = sampleArray[sampleArray.length - 1];
console.log(lastElement) // bar

PHP

$sampleArray = ["value1", "value2", "value3"];
$lastElement = $sampleArray[count($sampleArray) - 1];
echo $lastElement; //value3

Python

sampleArray = ["value1", "value2", "value3"]
lastElement = sampleArray[len(sampleArray) - 1]
print(lastElement)

Author

With 💓 by Asaolu Elijah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment