array_unique


php128 apg

REMOVES all duplicate values from an ARRAY.





This function preserves all keys.

If multiple elements compare equal under the given $sort_flags, then the key and value of the first equal element will be retained.

Two elements are considered equal when the string representation are the same, the first element will be used.

Since the PHP 7.2.0, if $sort_flags = 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.



<?php

arr array_unique 
( arr $array [, int $sort_flags = SORT_STRING ] )
 

where,

$array = The input ARRAY

$sort_flags = May be used to modify the sorting behavior
                     
( SEE the below TABLE )

?> 

$array


The input ARRAY.



$sort_flags


May be used to modify the sorting behavior using one of these values:

CONSTANT VALUE MEANING
SORT_REGULAR 0 Don't change types.
Compare items NORMALLY.
SORT_NUMERIC 1 Compare items NUMERICALLY.
SORT_STRING 2 Compare items as STRINGS.
SORT_LOCALE_STRING 5 Compare items as STRINGS.
Based on the LOCALE current.
ed48


  1 EXERCISE   

<?php

$arr01 
= array(8, 8, 8, 16, 32, 32, 64, 128);

print_r($arr01);

echo 
'<br><br><br>';

$narr01 = array_unique($arr01);

print_r($narr01);

?>  

 RESULT   

The given ARRAY
Array ( [0] => 8 [1] => 8 [2] => 8 [3] => 16 [4] => 32 [5] => 32 [6] => 64 [7] => 128 )

SORT_STRING
Array ( [0] => 8 [3] => 16 [4] => 32 [6] => 64 [7] => 128 )


  2 EXERCISE   

<?php

$arr02 
= ["A", "b", "a", "B", "x", "y", "z", "A"];

print_r($arr02);

echo 
'<br><br><br>';

$narr02 = array_unique($arr02, 5);

print_r($narr02);

?>  

 RESULT   

The given ARRAY
Array ( [0] => A [1] => b [2] => a [3] => B [4] => x [5] => y [6] => z [7] => A )

SORT_LOCALE_STRING
Array ( [0] => A [1] => b [2] => a [3] => B [4] => x [5] => y [6] => z )


  3 EXERCISE   

<?php

$arr03 
= [8, 8, 8, 16, 32, 32, 64, 128];

print_r($arr03);

echo 
'<br><br>';

$narr03r = array_unique($arr03, SORT_REGULAR);

print_r($narr03r);

echo 
'<br><br>';

$narr03n = array_unique($arr03, SORT_NUMERIC);

print_r($narr03n);

?>  

 RESULT   

The given ARRAY
Array ( [0] => 8 [1] => 8 [2] => 8 [3] => 16 [4] => 32 [5] => 32 [6] => 64 [7] => 128 )>

SORT_REGULAR
Array ( [0] => 8 [3] => 16 [4] => 32 [6] => 64 [7] => 128 )

SORT_NUMERIC
Array ( [0] => 8 [3] => 16 [4] => 32 [6] => 64 [7] => 128 )


  4 EXERCISE   

<?php

$arr04 
= ["A", "b", "a", "B", "x", "y", "z", "A"];

print_r($arr04);

echo 
'<br><br>';

$narr04r = array_unique($arr04, SORT_REGULAR);

print_r($narr04r);

echo 
'<br><br>';

$narr04n = array_unique($arr04, SORT_NUMERIC);

print_r($narr04n);

echo 
'<br><br>';

$narr04l = array_unique($arr04, SORT_LOCALE_STRING);

print_r($narr04l);

?>  

 RESULT   

The given ARRAY
Array ( [0] => A [1] => b [2] => a [3] => B [4] => x [5] => y [6] => z [7] => A )

SORT_REGULAR
Array ( [0] => A [1] => b [2] => a [3] => B [4] => x [5] => y [6] => z )

SORT_NUMERIC
Array ( [0] => A )

SORT_LOCALE_STRING
Array ( [0] => A [1] => b [2] => a [3] => B [4] => x [5] => y [6] => z )


  5 EXERCISE   

<?php
   
$arr05 
= [ "Attention", "you", "are", 
                
"prevented", "from leaving", 
                
"because of the coronavirus!", 
                
"Attention", "you", "are", 
                
"prevented", "from leaving" ];

echo 
'The given ARRAY:<br>';
print_r($arr05);

echo 
'<br><br>SORT_REGULAR:<br>';
$narr05r = array_unique($arr05, SORT_REGULAR);

print_r($narr05r);

echo 
'<br><br>SORT_NUMERIC:<br>';

$narr05n = array_unique($arr05, SORT_NUMERIC);

print_r($narr05n);

echo 
'<br><br>SORT_LOCALE_STRING:<br>';

$narr05l = array_unique($arr05, SORT_LOCALE_STRING);

print_r($narr05l);

?>  

 RESULT   

The given ARRAY
Array ( [0] => Attention [1] => you [2] => are [3] => prevented [4] => from leaving [5] => because of the coronavirus! [6] => Attention [7] => you [8] => are [9] => prevented [10] => from leaving )

SORT_REGULAR
Array ( [0] => Attention [1] => you [2] => are [3] => prevented [4] => from leaving [5] => because of the coronavirus! )

SORT_NUMERIC
Array ( [0] => Attention )

SORT_LOCALE_STRING
Array ( [0] => Attention [1] => you [2] => are [3] => prevented [4] => from leaving [5] => because of the coronavirus! )