I have the array:
array(2=>'0',3=>'1',5=>'3',4=>'4',7=>'2')
Note: Nothing seems to be in order. So leave the plan to find a numeric order.
How can I fixed the i'th element of this associative array?
For example when i
is 4
the value should be 4
,
when i
is 1
the value should be 0
.
array_shift()
never solves this requirement. Knowing very well that the logic of looping solves this problem.
This should work for you:
Here you just can use
array_keys()
to access the keys of your associative array as a numerical indexed array, which you then again can use as key for the array.output:
when the solution is a[i] <- You can't get it that simple, but you get close with the solution above. (Note, that since the array with the keys is 0-based index, the 4th element is index 3)
You can use a variable and then subtract one, to get your logic, that 4 => 4.