Elegant way to search an PHP array using a user-defined function

后端 未结 6 1428
感情败类
感情败类 2020-12-06 03:58

Basically, I want to be able to get the functionality of C++\'s find_if(), Smalltalk\'s detect: etc.:

// would return the element o         


        
6条回答
  •  时光取名叫无心
    2020-12-06 04:33

    To pull the first one from the array, or return false:

    current(array_filter($myArray, function($element) { ... }))
    

    More info on current() here.

提交回复
热议问题