efficient way to search object in an array by a property

前端 未结 7 1484
傲寒
傲寒 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 12:57

    This is my absolute favorite algorithm for very quickly finding what I need in a very large array, quickly. It is a Binary Search Algorithm implementation I created and use extensively in my PHP code. It hands-down beats straight-forward iterative search routines. You can vary it a multitude of ways to fit your need, but the basic algorithm remains the same.

    To use it (this variation), the array must be sorted, by the index you want to find, in lowest-to-highest order.

    function quick_find(&$array, $property, $value_to_find, &$first_index) {
        $l = 0;
        $r = count($array) - 1;
        $m = 0;
        while ($l <= $r) {
            $m = floor(($l + $r) / 2);
            if ($array[$m]->{$property} < $value_to_find) {
                $l = $m + 1;
            } else if ($array[$m]->{$property} > $value_to_find) {
                $r = $m - 1;
            } else {
                $first_index = $m;
                return $array[$m];
            }
        }
        return FALSE;
    }
    

    And to test it out:

    /* Define a class to put into our array of objects */
    class test_object {
        public $index;
        public $whatever_you_want;
        public function __construct( $index_to_assign ) {
            $this->index = $index_to_assign;
            $this->whatever_you_want = rand(1, 10000000);
        }
    }
    
    /* Initialize an empty array we will fill with our objects */
    $my_array = array();
    
    /* Get a random starting index to simulate data (possibly loaded from a database) */
    $my_index = rand(1256, 30000);
    
    /* Say we are needing to locate the record with this index */
    $index_to_locate = $my_index + rand(200, 30234);
    
    /* 
     * Fill "$my_array()" with ONE MILLION objects of type "test_object" 
     * 
     * 1,000,000 objects may take a little bit to generate.  If you don't
     * feel patient, you may lower the number!
     * 
     */
    for ($i = 0; $i < 1000000; $i++) {
        $searchable_object = new test_object($my_index); // Create the object
        array_push($my_array, $searchable_object); // Add it to the "$my_array" array
        $my_index++; /* Increment our unique index */
    }
    
    echo "Searching array of ".count($my_array)." objects for index: " . $index_to_locate ."\n\n";
    
    $index_found = -1; // Variable into which the array-index at which our object was found will be placed upon return of the function.
    
    $object = quick_find($my_array, "index", $index_to_locate, $index_found);
    
    if ($object == NULL) {
        echo "Index $index_to_locate was not contained in the array.\n";
    } else {
        echo "Object found at index $index_found!\n";
        print_r($object);
    }
    echo "\n\n";
    

    Now, a few notes:

    You MAY use this to find non-unique indexes; the array MUST still be sorted in ascending order. Then, when it finds an element matching your criteria, you must walk the array backwards to find the first element, or forward to find the last. It will add a few "hops" to your search, but it will still most likely be faster than iterating a large array.

    For STRING indexes, you can change the arithmetic comparisons (i.e. " > " and " < " ) in quick_find() to PHP's function "strcasecmp()". Just make sure the STRING indexes are sorted the same way (for the example implementation): Alphabetically and Ascending.

    And if you want to have a version that can search arrays of objects sorted in EITHER ascending OR decending order:

    function quick_find_a(&$array, $property, $value_to_find, &$first_index) {
        $l = 0;
        $r = count($array) - 1;
        $m = 0;
        while ($l <= $r) {
            $m = floor(($l + $r) / 2);
            if ($array[$m]->{$property} < $value_to_find) {
                $l = $m + 1;
            } else if ($array[$m]->{$property} > $value_to_find) {
                $r = $m - 1;
            } else {
                $first_index = $m;
                return $array[$m];
            }
        }
        return FALSE;
    }
    
    function quick_find_d(&$array, $property, $value_to_find, &$first_index) {
        $l = 0;
        $r = count($array) - 1;
        $m = 0;
        while ($l <= $r) {
            $m = floor(($l + $r) / 2);
            if ($value_to_find > $array[$m]->{$property}) {
                $r = $m - 1;
            } else if ($value_to_find < $array[$m]->{$property}) {
                $l = $m + 1;
            } else {
                $first_index = $m;
                return $array[$m];
            }
        }
        return FALSE;
    }
    
    
    function quick_find(&$array, $property, $value_to_find, &$first_index) {
        if ($array[0]->{$property} < $array[count($array)-1]->{$property}) {
            return quick_find_a($array, $property, $value_to_find, $first_index);
        } else {
            return quick_find_d($array, $property, $value_to_find, $first_index);
        }
    }
    
    0 讨论(0)
  • 2020-12-29 13:02

    You can use the function array_search of php like this

    $key=array_search("one", array_column(json_decode(json_encode($array),TRUE), 'color'));
    var_dump($array[$key]);
    
    0 讨论(0)
  • 2020-12-29 13:03

    You can iterate that objects:

    function findObjectById($id){
        $array = array( /* your array of objects */ );
    
        foreach ( $array as $element ) {
            if ( $id == $element->id ) {
                return $element;
            }
        }
    
        return false;
    }
    

    Edit:

    Faster way is to have an array with keys equals to objects' ids (if unique);

    Then you can build your function as follow:

    function findObjectById($id){
        $array = array( /* your array of objects with ids as keys */ );
    
        if ( isset( $array[$id] ) ) {
            return $array[$id];
        }
    
        return false;
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 13:11

    Here is what I use. Reusable functions that loop through an array of objects. The second one allows you to retrieve a single object directly out of all matches (the first one to match criteria).

    function get_objects_where($match, $objects) {
        if ($match == '' || !is_array($match)) return array ();
        $wanted_objects = array ();
        foreach ($objects as $object) {
            $wanted = false;
            foreach ($match as $k => $v) {
                if (is_object($object) && isset($object->$k) && $object->$k == $v) {
                    $wanted = true;
                } else {
                    $wanted = false;
                    break;
                };
            };
            if ($wanted) $wanted_objects[] = $object;
        };
        return $wanted_objects;
    };
    
    function get_object_where($match, $objects) {
        if ($match == '' || !is_array($match)) return (object) array ();
        $wanted_objects = get_objects_where($match, $objects);
        return count($wanted_objects) > 0 ? $wanted_objects[0] : (object) array ();
    };
    
    0 讨论(0)
  • 2020-12-29 13:15

    The thing with performance of data structures is not only how to get but mostly how to store my data.

    If you are free to design your array, use an associative array:

    $array['one']->id = 'one';
    $array['one']->color = 'white';
    $array['two']->id = 'two';
    $array['two']->color = 'red';
    $array['three']->id = 'three';
    $array['three']->color = 'blue';
    

    Finding is then the most cheap: $one = $array['one];

    UPDATE:

    If you cannot modify your array constitution, you could create a separate array which maps ids to indexes. Finding an object this way does not cost any time:

    $map['one'] = 0;
    $map['two'] = 1;
    $map['three'] = 2;
    ...
    

    getObjectById() then first lookups the index of the id within the original array and secondly returns the right object:

    $index = $map[$id];
    return $array[$index];
    
    0 讨论(0)
提交回复
热议问题