I'm looking for an efficient algorithm that allows me to search a multidimensional array for a specific value. I have something like this:
$stuff = array(
array(
'id' => 'abc123',
'name' => 'test'
'contents' => 'Lorem ipsum'
),
array(
'id' => 'abc123',
'name' => 'test'
'contents' => 'Lorem ipsum'
),
array(
'id' => 'abc123',
'name' => 'test'
'contents' => 'Lorem ipsum'
),
array(
'id' => 'abc43',
'name' => 'test'
'contents' => 'Lorem ipsum'
),
array(
'id' => 'xyzh31',
'name' => 'test'
'contents' => 'Lorem ipsum'
),
);
I have to find the item with id = abc43. How can i do this efficiently? Do you know a better algorithm than traversing the entire array?
I think you should use array_multisort() this is as efficient as it could get and I personally use it for searching values in matrices.
You can use array_multisort() as described before, and then implement a binary search algorithm. I will try to describe it in seudocode.