Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Array Find Object's Index

How would I figure out where a specific item is in an array? For instance I have an array like this:

("itemone", "someitem", "fortay", "soup")

How would I get the index of "someitem"

Thanks, Christian Stewart

like image 631
Christian Stewart Avatar asked Jun 03 '26 04:06

Christian Stewart


1 Answers

Use array_search()

array_search — Searches the array for a given value and returns the corresponding key if successful

mixed array_search ( mixed $needle , array $haystack [, bool $strict ] )

Example:

$key = array_search('someitem', $array);
like image 153
NullUserException Avatar answered Jun 05 '26 17:06

NullUserException



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!