As unikalizirovat an array of references to objects by reference, not by value objects?

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[0]++; // Course has changed the value of $array[1]: print_r($array); // Array ( [0] => 2 [1] => 2 [2] => 2 ) print_r(array_unique($array)); // Array ( [0] => 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[0] === $array[1] // true $array[0] === $array[2] // true :(

The answer to this question is suggested in the comments:
is_ref_to function(&$a, &$b) { $t = $a; if($r=($b===($a=1))){ $r = ($b===($a=0)); } $a = $t; return $r; }
by | 27 views

3 Answers

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) { [0]=> object(stdClass)#2 (1) { ["a"]=> int(1) } [1]=> object(stdClass)#3 (1) { ["a"]=> int(1) } [2]=> object(stdClass)#4 (1) { ["b"]=> int(3) } 
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,740 users