Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the search () method in the Laravel collection

Shulou Source: shulou.com Published: 2022-06-01 02:16:37 10月25日 Update

This article will explain in detail how to use the search () method in the Laravel collection. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Search ()

The search method can find the collection with a given value. If this value is in the collection, the corresponding key is returned. If no data item matches the corresponding value, false is returned.

$names = collect (['Alex',' John', 'Jason',' Martyn', 'Hanlin']); $names- > search (' Jason'); / / 2

The search method uses loose comparisons by default. You can use strict comparison by passing true in its second parameter.

You can also pass your own callback function to the search method. The key of the first item that passes the callback truth test is returned.

Names = collect (['Alex',' John', 'Jason',' Martyn', 'Hanlin']); $names- > search (function ($value, $key) {return strlen ($value) = = 6;}) / 3 this is the end of the article on "how to use the search () method in the Laravel collection". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Methods articles more good practical loose content functions parameters data data items articles truth values knowledge references help related testing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei NVidia Shulou Information Apple