简体   繁体   中英

Laravel / PHP how to find the sequence (1th, 2nd, 3d, nth) of a value in collection

So my aim is to check if a value exist in Collection and then get the sequence of that value in that collection like if its 1st or 20th or 243th. We can find if value exist in collection with "contains" but the sequence?.. I tried to search and look how to get it but I couldnt find anything useful.

Any ideas how I can do that?

To find the index of the item you're looking for, you could use the search method. The Laravel documentation gives this example:

$collection = collect([2, 4, 6, 8]);

$collection->search(4);

The search method here will return 1 which refers to the first index that the searched for value 4 was found. Similarly you could do $collection->search(8) and it would return 3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM