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

To clarify the note of the array_unique function #4370

Open
mmalferov opened this issue Jan 7, 2025 · 0 comments
Open

To clarify the note of the array_unique function #4370

mmalferov opened this issue Jan 7, 2025 · 0 comments

Comments

@mmalferov
Copy link
Member

mmalferov commented Jan 7, 2025

The note in the unique_array() page says:

If 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.

(This sentense is mentioniong in Backward incompatible changes 7.1.x to 1.2.x page and Changelog too.)

The following questions arise:

  • What is an "the array packing"? I suppose it is the C term that means resizing an array to eliminate gaps or something similar.
  • How exactly does removing duplicates from an internal copy of an array without afterwards packing the array affect numeric indexes? Does the C language preserve the size of the internal array, preserve the maximum numeric index, or any other effect? And most importantly,—what should this tell a PHP programmer who is unfamiliar with the terms "the array packing"?
  • Due to the lack of information about the internally packing of the array in the sentence "This can result in different numeric indexes", it is unclear: a) what "this" indicates b) in which specific cases numeric indexes may differ (although, maybe this is an unimportant question, it just happens sometimes) c) and what exactly from what exactly may differ?

I propose to reformulate the note. At least according to the following formula:

If flags is SORT_STRING, formerly array has been copied and non-unique elements have been removed (without packing the array afterwards [AND WHAT?]), but now a new array is built by adding the unique elements. This [WHAT EXACTLY?] can result [IN WHICH CACES?] in different numeric indexes [BETWEEN WHAT AND WHAT].

I know that, prior to PHP 7.2.0, after the array_unique function filters an input array, the last internal numeric index of the array copy don't reset, and, after adding a new value in the filtered array, the next numeric index gets the next value after the 'last maximum used numeric index':

<?php

$a = [
	0 => 0,
	100 => 0,
];

$b = array_unique($a, SORT_STRING);

$b[] = 1;

print_r($b);

Output prior to PHP 7.2.0:

Array
(
[0] => 0
[101] => 1 // Note: the index is 101, but not 1 (this is probably a consequence of the lack of array packing.)
)

Output as of PHP 7.2.0:

Array
(
[0] => 0
[1] => 1
)

Did the words "different numeric indexes" mean this difference?

@mmalferov mmalferov changed the title To clarify the function array_unique note To clarify the note of the array_unique function Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant