What you really want is set , not an array. Therefore, if you cannot fix the way you build the array in the first place (I assume that it came from an SQL query, for which there would be much less code to fix), you have two options for creating mapped sets in PHP.
- You can use
SplObjectStorage - You can use
Array with a key as a serialized set view
The first approach would look something like this:
$set = new SplObjectStorage(); $arr = [ 0 => [ 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ], 1 => [ 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ], 2 => [ 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ], 3 => [ 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ], 4 => [ 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ], ]; foreach($arr as $part) { if (isset($part['altunit'])) {
It will give you ...
object(stdClass)#2 (3) { ["id"]=> string(1) "2" ["altunit"]=> string(1) "%" ["measurement"]=> NULL }
The second approach, using an array, where the array key represents a serialized version of the set, would look something like this:
$set = []; $arr = [ 0 => [ 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ], 1 => [ 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ], 2 => [ 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ], 3 => [ 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ], 4 => [ 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ], ]; foreach($arr as $part) { if (isset($part['altunit'])) {
And that will give you ...
array(3) { ["id"]=> string(1) "2" ["altunit"]=> string(1) "%" ["measurement"]=> NULL }
NB
But, if this is the result of a set from an SQL query, it is possible you could just more efficiently eliminate the deduplication process completely by modifying the query to use the WHERE NOT NULL and GROUP BY clauses instead.