Hello. There is a certain two-dimensional array:

Array( [0]=>Array([id]=>1[name]=>Вип) [1]=>Array([id]=>2[name]=>Премиум) ) 

How it will be more correct to receive value name , knowing id ?
The fact is that the array can be very large, and this operation can occur more than once: I would like this to not affect the speed.

    2 answers 2

    ID is unique? if yes, then how did you find the break so as not to scroll further, but in general if the array is always one and there are a lot of searches for it, then sort it in any way (push it into the tree) there search is much faster.

    • Can you please tell us more about the tree? Or give a link where to read about it. - Mr_Epic
    • @Mr_Epic, or a hash function, add me to Skype, tell you more about Manitikyl - Manitikyl
    • @Mr_Epic ru.wikipedia.org/wiki ... the bottom line is that each node has a maximum of two child nodes, the left one will be less than the parent, and the right one more. Having a root node in your hands, you can easily reach the desired search location by comparing the values ​​of the desired number with the value of the current node and going to the right or left. Such an organization makes it possible to shorten the path to the desired element from N to log N operations, if the memory does not fail (to calculate in this state I do not calculate, excuse me) - etki

    If I understand correctly, then just go through the array, check the id and print the name.

     $id = 2; $arr = array( array('id' => 1, 'name' => 'vip'), array('id' => 2, 'name' => 'premium') ); foreach ($arr as $k => $v) { $return = ($v['id'] == $id) ? $v['name'] : null; echo $return; } 
    • And there are no other options, except for iterating over the entire array? - Mr_Epic
    • @Mr_Epic did not see this :) - Bastiane
    • @Mr_Epic, when you get to the array, it acts as a black box for you, there are some elements, but it is not known what is in them. Are there any more ways to learn, except to look into each? Here, except to change the method itself (DB) or output (tree, value hashes) of receiving data. - etki