我有一個字符串數組。不知道是否有簡單的方法來獲得在數組中找到的項目的索引?PS:獲取數組列表中的索引
# example array
$array = "A", "B", "C"
$item = "B"
# the following line gets null, any way to get its index?
$index = $array | where {$_ -eq $item} | ForEach-Object { $_.Index }
我可以做一個循環。不確定是否有其他方法?
不錯。如果數組中有兩個「B」,則結果將是一個索引值數組。 – 2009-12-22 18:15:44