0 like 0 dislike
27 views
``\$a = 1; \$b = 2; \$array[] = &\$a; // Two elements - a reference to \$a \$array[] = &\$a; \$array[] = &\$b; // Single element - the reference to \$b \$array++; // Course has changed the value of \$array: print_r(\$array); // Array (  => 2  => 2  => 2 ) print_r(array_unique(\$array)); // Array (  => 2 ) // Three links, two of which are unique, there is only one``

I require that after operation the unique output was an array that contains one reference to \$a and one \$b.

How to implement?

Thank you!

Upd:

How do you compare two references to the equivalence?

For the code above:
``\$array === \$array // true \$array === \$array // true :(``

``is_ref_to function(&\$a, &\$b) { \$t = \$a; if(\$r=(\$b===(\$a=1))){ \$r = (\$b===(\$a=0)); } \$a = \$t; return \$r; }``
| 27 views

0 like 0 dislike
To write his version of array_unique, for comparison within the library is on string values (string)s1 == (string)s2.
by
0 like 0 dislike
spl_object_hash is not suitable?
\r
Would be something like:
\$id = spl_object_hash(\$a);
\$array[\$id] = \$a;
by
0 like 0 dislike
``\$s = new SplObjectStorage(); \$o1 = new StdClass; \$o1->a = 1; \$o2 = new StdClass; \$o2->a = 1; \$o3 = new StdClass; \$o3->b = 3; \$s->attach(\$o1); \$s->attach(\$o2); \$s->attach(\$o1); \$s->attach(\$o3); var_dump(iterator_to_array(\$s)); ``
``array(3) { => object(stdClass)#2 (1) { ["a"]=> int(1) } => object(stdClass)#3 (1) { ["a"]=> int(1) } => object(stdClass)#4 (1) { ["b"]=> int(3) } ``
by

0 like 0 dislike