Write a PHP program to remove duplicate values from an array which contains only strings or only integers

  • برمجة بي اتش بي

Write a PHP program to remove duplicate values from an array which contains only strings or only integers.

الأجوبة

<?php
$colors = array( 
  0 => 'Red', 
  1 => 'Green', 
  2 => 'White', 
  3 => 'Black', 
  4 => 'Red', 
); 

$numbers = array( 
  0 => 100, 
  1 => 200, 
  2 => 100, 
  3 => -10, 
  4 => -10, 
  5 => 0, 
); 
$uniq_colors = array_keys(array_flip($colors)); 
$uniq_numbers = array_keys(array_flip($numbers)); 
print_r($uniq_colors);
print_r($uniq_numbers);
?>

Sample Output:

Array                                                       
(                                                           
    [0] => Red                                              
    [1] => Green                                            
    [2] => White                                            
    [3] => Black                                            
)                                                           
Array                                                       
(                                                           
    [0] => 100                                              
    [1] => 200                                              
    [2] => -10                                              
    [3] => 0                                                
)
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...