gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
ihave array in php and i need to display position by value
- User
ihave array in php and i need to display position by value
- Assistant
To display the position of a value in an array in PHP, you can use the array_search() function. This function returns the key for the first occurrence of the value in the array.
Here is an example code:
code$myArray = array('apple', 'banana', 'orange', 'kiwi'); $pos = array_search('orange', $myArray); echo "The position of 'orange' in the array is: " . $pos;Output:
codeThe position of 'orange' in the array is: 2In this example, the array_search() function is used to search for the value 'orange' in the $myArray array. The result is the position of the value in the array, which is then displayed using the echo statement.