What change does array_unique() make when migrating to PHP 7.2?

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Tue Dec 04, 2018 2:06 am

Here is the description of array_unique() in PHP 7.2:
If sort_flags is SORT_STRING, formerly array has been copied and non-unique elements have been removed (without packing the array afterwards), but now a new array is built by adding the unique elements. This can result in different numeric indexes.
I have done many examples to see if there are any difference but can't. Could anyone please help show me the changes?
Thank you.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 04, 2018 2:29 am

The function name describes its function perfectly.

What you have quoted is a change that applies to PHP 7.2, it needs to be read in conjunction with the original description and not taken as a unique description of the function.

tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Tue Dec 04, 2018 2:36 am

hyper wrote:
Tue Dec 04, 2018 2:29 am
The function name describes its function perfectly.

What you have quoted is a change that applies to PHP 7.2, it needs to be read in conjunction with the original description and not taken as a unique description of the function.
Sorry for not asking the problem clearly. What I want to ask is in the title of the topic: What changes does it (array_unique) make when Upgrading PHP from 7.1 to 7.2?
More specifically, what does this sentence mean: "This can result in different numeric indexes."
Thank you.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 04, 2018 12:56 pm

Are you getting problems using the function since the change?

tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Tue Dec 04, 2018 4:52 pm

hyper wrote:
Tue Dec 04, 2018 12:56 pm
Are you getting problems using the function since the change?
I'm on the way to migrate the whole system of the product.
So I want to know what changes it could possibly make. (We should guess the problem then deal with it first, not waiting for customer to complain about bug & fix it later)

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Wed Dec 05, 2018 10:39 am

I'm on the way to migrate the whole system of the product.
Without describing your use and what migration means in this case, it is very difficult to give a helpful answer to your potential problem

If you're that concerned, in my opinion, you'd be better off looking at another way of doing whatever it is you are doing.

tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Fri Dec 07, 2018 1:30 am

hyper wrote:
Wed Dec 05, 2018 10:39 am
I'm on the way to migrate the whole system of the product.
Without describing your use and what migration means in this case, it is very difficult to give a helpful answer to your potential problem

If you're that concerned, in my opinion, you'd be better off looking at another way of doing whatever it is you are doing.
I think you don't understand my point here. Let me make it clear again for you:
- My product is big, and it has about 150+ usages of array_unique() function.
- The product is now using PHP 7.1, and the migration process is to update it to PHP 7.2
- My task is to investigate what changes of PHP 7.2 may affect my product (http://php.net/manual/en/migration72.php). array_unique() has changed something in PHP 7.2, it is described in here (http://php.net/manual/en/migration72.incompatible.php), but I don't know what this sentence means:
This can result in different numeric indexes.
I said I dont know it because when I test array_unique() with numeric array/associative array in both PHP 7.1 & 7.2, nothing seems to be changed about the index key.

Hope that you can understand my question and have some ideas.
Thank you.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Fri Dec 07, 2018 7:03 am

I think you don't understand my answer here. Let me make it clear again for you:

You have run your tests and found no difference, my reading of the sentence is:

Code: Select all

<?php

$array = ['apple', 'banana', 'grape', 'banana','Kiwi'];
var_dump($array);

var_dump(array_unique($array));
($array);

/* Output
array(5) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(5) "grape"
  [3]=>
  string(6) "banana"
  [4]=>
  string(4) "Kiwi"
}
array(4) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(5) "grape"
  [3]=>
  string(4) "Kiwi"
}
*/
The use of the word 'Can' means that it might (or might not always) do that, which is what I found when running the above, which is where I presume your concern comes in. This is where you might have to think outside of the box.

When referring to your use of array_unique, I meant that - if the indexes are not used in your code, then it makes no difference.

When testing your code, have you found any problems using PHP 7.2 that didn't exists before?
it has about 150+ usages of array_unique()
Do you mean that you use it 150+ times, in 150+ ways or in 150+ places?

tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Sun Dec 09, 2018 5:42 pm

hyper wrote:
Fri Dec 07, 2018 7:03 am
I think you don't understand my answer here. Let me make it clear again for you:

You have run your tests and found no difference, my reading of the sentence is:

.....

Code: Select all

<?php

$array = ['apple', 'banana', 'grape', 'banana','Kiwi'];
var_dump($array);

var_dump(array_unique($array));
($array);

/* Output
array(5) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(5) "grape"
  [3]=>
  string(6) "banana"
  [4]=>
  string(4) "Kiwi"
}
array(4) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(5) "grape"
  [3]=>
  string(4) "Kiwi"
}
*/
Sorry for troubling you but could you please give me the site you tested this (online site is great), or did you tested this by your local site?
I have tried in many online PHP online site in different version (5.0, 7.0, 7.1, 7.2) but the result always comes as: Array key of 'Kiwi' is 4, not 3 as your result.
The use of the word 'Can' means that it might (or might not always) do that, which is what I found when running the above, which is where I presume your concern comes in. This is where you might have to think outside of the box.
This sentences of yours might answer my problem. But it's very hard to understand this change if it goes like that. Why would they change a consistent function to something that is inconsistent? :(
Do you mean that you use it 150+ times, in 150+ ways or in 150+ places?
I mean 150+ places

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 10, 2018 9:01 am

Sorry, I wasn't clear enough; the result I got was:

Code: Select all

array(5) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(5) "grape"
  [4]=>
  string(4) "Kiwi"
}
And it was on my own development PC running V7.2.10.

As hard as the developers try, it sometimes happens that certain bits don't work completely as expected due to slight changes in operating system and patches etc.. used to get the thing working.

I don't understand why you would have a problem if you are already working with the above scenario verses the indexes being re-numbered?

tungps250991
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 04, 2018 2:03 am

Mon Dec 10, 2018 5:43 pm

hyper wrote:
Mon Dec 10, 2018 9:01 am
...

I don't understand why you would have a problem if you are already working with the above scenario verses the indexes being re-numbered?
My problem is not about the current result when running the product. I just want to know for sure if the change they've made MAY cause any impact/future bug on my current source code or not, so I can find the code that need to be refactored.
The result in your/my test code cannot satisfy me about the change they've written. I need to see a test that show the difference.
I also asked on stackoverflow and this should answer to my problem (or anyone else's):

Code: Select all

https://stackoverflow.com/questions/53610207/what-change-does-array-unique-make-when-migrating-to-php-7-2?noredirect=1#comment94282965_53610207
Hope that will make it clear.

Post Reply