PHP 数组中值出现的第一个和最后一个索引

Pra*_*eep -1 php arrays

我在 PHP 中创建了一个数组,如下所示:

$arr = array(123,144,144,123);

如何获得每个值出现的第一个和最后一个索引。
就像是,

123 -> first occurrence - 0th index | last occurrence - 3rd index
144 -> first occurrence - 1st index | last occurrence - 2nd index
Run Code Online (Sandbox Code Playgroud)

Dan*_*ler 7

使用array_search()

$arr = array(123,144,144,123);
$first = array_search(123, $arr); // 0
$last = array_search(123, array_reverse($arr, true)); // 3
Run Code Online (Sandbox Code Playgroud)

阅读更多:http : //php.net/manual/en/function.array-search.php