Question: For some reason, I have a sorted php array:

```
"$arr_questions" = Array [6]    
    0   Array [6]   
    1   Array [6]   
    2   Array [6]   
    3   Array [6]   
    4   Array [6]   
    5   Array [6]   

```

each of the positions is another array. This time it is associative. See position [0]:

```
0 = Array [6]   
    question_id 40  
    question    La tercera pregunta del mundo   
    explanation 
    choices Array [3]   
    correct 0   
    answer  1   

```

Without looping my array, is there any way to access directly this position 0, just knowing one of its properties?

Example... Imagine I have to change some property of the position of the array whose "question\_id" property is 40. That is the only I know. I don't know if the question\_id property is gonna be in the first or second or which position. And, for example, imagine I want to change the "answer" property to 2.

How can I access directly to that position without looping the whole array. I mean... I don't want to do this:

```
foreach ($arr_questions as $question){
  if ($question["question_id"] == 40){
    $question["answer"] == 2;
  }
}

```

Answer: 
A PHP Array lets you access random values by its id.

It is actually a big deal, because in other languages array indices must be always integers.
However, PHP arrays work mostly like other-languages dictionaries, in which your key can be of other data types, like strings.

By that, if you want to be able to access some question, and you know the ID, then you should have constructed the array by letting your question\_id be the index of each array entry.

If you can't do that, don't panic.
In the end you will have to make some kind of search, that's true.
But hey, then you have two cases:

* a) Your array is **big**. Wow, in that case, you should run an optimized sorting algorithm, such as mergesort or quicksort, so that you can order your data quickly and then have them already sorted by your wanted field.
* b) Your array is not-so-big. I think in that case it's no big deal, and the sorting can slow your application more than it should, and if you want to be quicker, you should cache the results of sorting the questions (if possible) or refactoring the array construction so it uses your wanted key as the array index.

As a side note, you can't map things avoid wasting some CPU time or some RAM space, and usually you can swap one for the other.
I mean, if you store just one array indexed by question\_id then you can look up for question\_id's in O(1) + O(array-access) time. If O(array-access) is a constant, then you can get to things in O(1). That means constant time, and it is as fast as it can get.

However, if you need other kind of searches you can end up with O(n \* log(n)) or O(n²) time complexity.

But, if you had stored as many arrays as ways to order them you should need, you would need only O(1) time to access each of them. *But*, you would need O(n) space (where n here is the num of features to have direct access to).

That would increment the time to build the arrays (by a constant).
No, there is no way to access that element without looping over your array. You might abstract that search into a helper function, however.