foo() > 10; });But I don"t know of any PHP function which does this" /> foo() > 10; });But I don"t know of any PHP function which does this" />

Php Array_Search, Array Sort Callback Php Code Example

Basically, I want to lớn be able lớn get the functionality of C++"s find_if(), Smalltalk"s detect: etc.:

// would return the element or nullcheck_in_array($myArray, function($element) return $elemnt->foo() > 10; );But I don"t know of any PHP function which does this. One "approximation" I came up with:

$check = array_filter($myArray, function($element) ... );if ($check) //...The downside of this is that the code"s purpose is not immediately clear. Also, it won"t stop iterating over the array even if the element was found, although this is more of a nitpick (if the data phối is large enough khổng lồ cause problems, linear tìm kiếm won"t be an answer anyway)

php collections functional-programming
tóm tắt
Improve this question
asked Jan 8, 2013 at 22:03

4,28911 gold badge1818 silver badges3939 bronze badges
add a bình luận |

6 Answers 6

Sorted by: Reset to default
Trending sort available

Introducing: Trending sort You can now choose lớn sort by Trending, which boosts votes that have happened recently, helping to lớn surface more up-to-date answers.

Bạn đang xem: Php array_search, array sort callback php code example

Trending is based off of the highest score sort and falls back to it if no posts are trending.

Try it Dismiss
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
To pull the first one from the array, or return false:

current(array_filter($myArray, function($element) ... ))More info on current() here.

Xem thêm: Sm Là Gì Và Người Mang Nhóm Máu S M Là Người Như Thế Nào? Trường Hợp Sm Nguy Hiểm Chết Người

chia sẻ
Improve this answer
answered Jan 8, 2013 at 22:09

8,67222 gold badges3939 silver badges4949 bronze badges
| Show 4
more comments
Here"s a basic solution

function array_find($xs, $f) foreach ($xs as $x) if (call_user_func($f, $x) === true) return $x; return null;array_find(<1,2,3,4,5,6>, function($x) return $x > 4; ); // 5array_find(<1,2,3,4,5,6>, function($x) return $x > 10; ); // nullIn the sự kiện $f($x) returns true, the loop short circuits & $x is immediately returned. Compared khổng lồ array_filter, this is better for our use case because array_find does not have to continue iterating after the first positive match has been found.

In the sự kiện the callback never returns true, a value of null is returned.

Note, I used call_user_func($f, $x) instead of just calling $f($x). This is appropriate here because it allows you khổng lồ use any compatible callable

Class Foo static private $data = "z"; static public function match($x) return $x === self::$data; array_find(<"x", "y", "z", 1, 2, 3>, <"Foo", "match">); // "z"Of course it works for more complex data structures too

$data = < (object) <"id" => 1, "value" => "x">, (object) <"id" => 2, "value" => "y">, (object) <"id" => 3, "value" => "z">>;array_find($data, function($x) return $x->id === 3; );// stdClass Object (// => 3// => z// )If you"re using PHP 7, địa chỉ some type hints