Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with arrays #106

Open
mahoho opened this issue Oct 30, 2017 · 1 comment
Open

Working with arrays #106

mahoho opened this issue Oct 30, 2017 · 1 comment
Labels

Comments

@mahoho
Copy link

mahoho commented Oct 30, 2017

Hello!
I have some troubles when working with arrays.
When load pure array like this one:
'months' => ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
do some translations and then publish here is what i get:
'months' => [ '0' => 'Январь', '1' => 'Февраль', '10'=> 'Ноябрь', '11'=> 'Декабрь', '2' => 'Март', '3' => 'Апрель', '4' => 'Май', '5' => 'Июнь', '6' => 'Июль', '7' => 'Август', '8' => 'Сентябрь', '9' => 'Октябрь', ]
It literally rewrites all keys and makes it completely unusable, because further this array is converted to JS object, instead of array. Is there a way to avoid it and preserve array as is?
Thank you!

@mahoho mahoho changed the title Working Working with arrays Oct 30, 2017
@vsch
Copy link
Owner

vsch commented Oct 31, 2017

@mahoho, in PHP arrays and associative arrays are equivalent for numeric keys. At least when keys are start at 0 and numerically increasing by 1.

$i = [ 'a', 'b', 'c' ];
$j = [ '0' => 'a', '1' => 'b', '2' => 'c' ];
var_dump($i);
var_dump($j);

results in output:

php > var_dump($i);
php shell code:1:
array(3) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "b"
  [2] =>
  string(1) "c"
}
php > var_dump($j);
php shell code:1:
array(3) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "b"
  [2] =>
  string(1) "c"
}

which shows that the two declarations are equivalent. However, I noticed that the keys in the translations are not created in numeric order which causes the two arrays to differ:

$i = [ 'a', 'b', 'c' ];
$j = [ '0' => 'a', '2' => 'c', '1' => 'b' ];
var_dump($i);
var_dump($j);

results in output:

php > var_dump($i);
php shell code:1:
array(3) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "b"
  [2] =>
  string(1) "c"
}
php > var_dump($j);
php shell code:1:
array(3) {
  [0] =>
  string(1) "a"
  [2] =>
  string(1) "c"
  [1] =>
  string(1) "b"
}

So the solution would require creating arrays with numerical keys sorted numerically and not alphabetically. This will ensure that if the keys are numeric and increasing by 1 then the created array will be equivalent to an array instead of associative array.

I will make a fix for this in the next release.

@vsch vsch added the ☹ bug label Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants