--- date: 2017-07-29T18:19:29+01:00 description: "Function bash array_search(), equivalent to PHP" draft: false tags: ['Bash', 'array'] title: "Bash : function array_search()" translationKey: "bash-array-search" --- ## Description `array_search()`: Searches the array for a given value and returns the corresponding key if successful *Équivalent à la function [[PHP array_search](http://php.net/manual/en/function.array-search.php)* ## Source Code {{< code "dev-bash-function-array-search-src" bash >}} ### Parameters * `needle` is the searched value * `haystack` is the array where to search ### Return Values * Returns the key for `needle`, if is found into the array `haystack` * Otherwise, returns `1`: considere this value as `FALSE`. ## Example `declare -a color=("blue", "red", "green", "grey");`
`echo "$(array_search "red" "${color[@]}")"` ---