Change language

PHP Array_unique () function

In addition, according to this function, two elements are considered equal if and only if (string) $elem1 === (string) $elem2, i.e. when the string representation of the elements is the same.Syntax :
array array_unique ($array, $sort_flags)
Note : Array keys are saved. That is, the keys of non-deleted elements of the input array will be the same in the output array.Parameters : this function takes two parameters, one of which is required and the other - optional. Both of these parameters are described below:
  • $array : This parameter is required and defines the input array from which we want to remove duplicates.
  • $sort_flags : This is an optional parameter. This $sort_flags parameter can be used to change the sorting behavior using these values:
    • SORT_REGULAR: This is the default value for the $sort_flags parameter. This value tells the function to compare elements normally (do not change types).
    • SORT_NUMERIC: This value tells the function to compare elements numerically.
    • SORT_STRING: This value tells the function to compare elements as strings.
    • SORT_LOCALE_STRING: This value tells the function to compare elements as strings based on the current locale.
  • Return value: array_unique() function returns the filtered array after removing all duplicates from the array.The following programs illustrate the array_unique() function in PHP:Example-1 :  
    // Input Array $a = array ( "red" , "green" , " red " , " blue " );  
    // Array after removing duplicates print_r ( array_unique ( $a ));  
    ?>
    Output:
    Array ([ 0] = > red [1] = > green [3] = > blue)
    Example-2 :  
    // input array $arr = array ( "a" = > " MH " , " b " = > "JK" , "c" = > "JK" , "d" = > "OR " );  
    // Array after removing duplicates print_r ( array_unique ( $arr ));  
    ?>
    Output:
    Array ([ a] = > MH [b] = > JK [d] = > OR)
    Important points to pay attention to:
    • Array_unique() is not designed to work with multidimensional arrays.
    • The keys of the input array are preserved.
    • According to this function, two elements are considered equal if their string representation is the same .
    Link :
    HTTP : HTTP: //php.net/manual/en/function.array-unique.phpp

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically