I have the following array:
[answer] => Array
(
[0] => A
[2] => E
)
I run the following code:
for ($i = 0; $i < 3; $i++){
if (!isset(answer[$i]))answer[$i] = "X";
}
and get the following:
[answer] => Array
(
[0] => A
[2] => E
[1] => X
)
Is there a way to preserve the order so we get:
[answer] => Array
(
[0] => A
[1] => X
[2] => E
)
2
Answers
You can use ksort and it will sort the array in place.
https://www.php.net/manual/en/function.ksort.php
I think the simplest way is to sort array using ksort: https://www.php.net/manual/en/function.ksort.php.
Or if you don’t want to sort for some reason, you can use another array to store data in order you expect:
Output for two methods (as expected):