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;
}
}
With your situation it’s not possible without a loop, but if you change your array structure to this:
Which 39 and 40 are your
question_id, then you can access them so fast without any loop.If you want or have to to keep that structure, then just write a function to get the
array, theassociative indexand thevalueyou want as parameters to search the array and return the found index, so you will not be forced to write this loop over and over …