Skip to content

Implement ARRAY_UNIQUE_UNSORTED option #9788

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4559,6 +4559,43 @@ PHP_FUNCTION(array_unique)
return;
}

if (sort_type == PHP_ARRAY_UNIQUE_UNSORTED) {
zend_long input_num_key;
zend_string *input_str_key;
zval *input_val;

array_init(return_value);
HashTable *result_ht = Z_ARR_P(return_value);

ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), input_num_key, input_str_key, input_val) {
zval *input_val_deref = input_val;
ZVAL_DEREF(input_val_deref);

zval *result_val;
ZEND_HASH_FOREACH_VAL(result_ht, result_val) {
ZVAL_DEREF(result_val);
if (fast_is_identical_function(input_val_deref, result_val)) {
goto skip_unsorted;
}
} ZEND_HASH_FOREACH_END();

if (UNEXPECTED(Z_ISREF_P(input_val) && Z_REFCOUNT_P(input_val) == 1)) {
ZVAL_DEREF(input_val);
}
Z_TRY_ADDREF_P(input_val);

if (input_str_key) {
zend_hash_add_new(Z_ARRVAL_P(return_value), input_str_key, input_val);
} else {
zend_hash_index_add_new(Z_ARRVAL_P(return_value), input_num_key, input_val);
}

skip_unsorted:;
} ZEND_HASH_FOREACH_END();

return;
}

cmp = php_get_data_compare_func_unstable(sort_type, 0);

RETVAL_ARR(zend_array_dup(Z_ARRVAL_P(array)));
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
*/
const SORT_FLAG_CASE = UNKNOWN;

/**
* @var int
* @cvalue PHP_ARRAY_UNIQUE_UNSORTED
*/
const ARRAY_UNIQUE_UNSORTED = UNKNOWN;

/**
* @var int
* @cvalue PHP_CASE_LOWER
Expand Down
3 changes: 2 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ PHPAPI bool php_array_pick_keys(const php_random_algo *algo, php_random_status *
#define PHP_SORT_LOCALE_STRING 5
#define PHP_SORT_NATURAL 6
#define PHP_SORT_FLAG_CASE 8
//#define PHP_SORT_DONT_USE 9

// Must not clash with PHP_SORT_ constants
#define PHP_ARRAY_UNIQUE_UNSORTED 9

#define PHP_COUNT_NORMAL 0
#define PHP_COUNT_RECURSIVE 1
Expand Down
83 changes: 83 additions & 0 deletions ext/standard/tests/array/array_unique_unsorted.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
--TEST--
array_unique() with ARRAY_UNIQUE_UNSORTED
--FILE--
<?php

enum Foo {
case Bar;
case Baz;
}

$bar = Foo::Bar;
$bar2 = Foo::Bar;

var_dump(array_unique([
Foo::Bar,
Foo::Baz,
Foo::Baz,
Foo::Bar,
], ARRAY_UNIQUE_UNSORTED));

var_dump(array_unique([
Foo::Bar,
Foo::Bar,
Foo::Baz,
], ARRAY_UNIQUE_UNSORTED));

var_dump(array_unique([
'a' => Foo::Bar,
'b' => Foo::Baz,
'c' => Foo::Bar,
], ARRAY_UNIQUE_UNSORTED));

var_dump(array_unique([
&$bar,
Foo::Bar,
&$bar2,
Foo::Baz,
], ARRAY_UNIQUE_UNSORTED));

$value2 = "hello";
$value3 = 0;
$value4 = &$value2;
var_dump(array_unique([
0,
&$value4,
&$value2,
"hello",
&$value3,
$value4
], ARRAY_UNIQUE_UNSORTED));

?>
--EXPECT--
array(2) {
[0]=>
enum(Foo::Bar)
[1]=>
enum(Foo::Baz)
}
array(2) {
[0]=>
enum(Foo::Bar)
[2]=>
enum(Foo::Baz)
}
array(2) {
["a"]=>
enum(Foo::Bar)
["b"]=>
enum(Foo::Baz)
}
array(2) {
[0]=>
&enum(Foo::Bar)
[3]=>
enum(Foo::Baz)
}
array(2) {
[0]=>
int(0)
[1]=>
&string(5) "hello"
}