The JavaScript Array lastIndexOf function is used to find the last occurrence of a specified element in an array. It searches the array from the end to the beginning and returns the index of the last occurrence of the specified element. If the element is not found in the array, it returns -1. The lastIndexOf function can also take an optional second parameter that specifies the starting index for the search. This function is useful when you need to find the last occurrence of an element in an array, especially when the array contains duplicate elements. Keep reading below to learn how to Javascript Array lastIndexOf in PHP.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Javascript Array lastIndexOf in PHP With Example Code

JavaScript’s `lastIndexOf()` method is a useful tool for finding the last occurrence of a specified element in an array. However, what if you’re working in PHP and need to perform a similar operation? Fortunately, PHP has a built-in function that can help: `array_search()`.

The `array_search()` function searches an array for a given value and returns the corresponding key if successful. If the value is not found, the function returns `false`. By default, `array_search()` performs a loose comparison, meaning that it will match values that are equal in value but not necessarily in type.

To use `array_search()` to find the last occurrence of a value in an array, you can simply reverse the array using the `array_reverse()` function and then search for the value using `array_search()`. Here’s an example:


$fruits = array('apple', 'banana', 'orange', 'banana', 'kiwi');
$last_banana = array_search('banana', array_reverse($fruits, true));
echo $last_banana; // outputs 3

In this example, we have an array of fruits that includes two occurrences of the string “banana”. We use `array_reverse()` to reverse the order of the array, and then pass the reversed array to `array_search()` along with the value we’re searching for. The `true` parameter passed to `array_reverse()` preserves the original keys of the array, which allows us to correctly identify the index of the last occurrence of “banana”.

By using `array_reverse()` and `array_search()`, you can easily find the last occurrence of a value in a PHP array.

Equivalent of Javascript Array lastIndexOf in PHP

In conclusion, the equivalent function of Javascript’s Array lastIndexOf in PHP is the array_search() function. This function allows you to search for the last occurrence of a value in an array and returns the corresponding key. It is a useful tool for developers who work with PHP and need to search for values in arrays. By using this function, you can easily find the last occurrence of a value in an array without having to write complex code. Overall, the array_search() function is a valuable addition to PHP’s array functions and can save developers time and effort in their coding projects.

Contact Us