efficient way to search object in an array by a property

前端 未结 7 1563
傲寒
傲寒 2020-12-29 12:36

well, having something like:

$array[0]->id = \'one\';
$array[0]->color = \'white\';
$array[1]->id = \'two\';
$array[1]->color = \'red\';
$array[2         


        
7条回答
  •  暖寄归人
    2020-12-29 13:03

    Well, you would would have to loop through them and check compare the ID's unless your array is sorted (by ID) in which case you can implement a searching algorithm like binary search or something of that sort to make it quicker.

    My suggestion would be to first sort the arrays using a sorting algorithm (binary sort, insertion sort or quick sort) if the array is not sorted already. Then you can implement a search algorithm which should improve performance and I think that's as good as it gets.

    http://www.algolist.net/Algorithms/Binary_search

提交回复
热议问题