Answers for "array_unique to string php"

PHP
2

php array unique array to string conversion

//if you have this error it's because array_unique() compare content of array
//as strings, but if your array contains other array the function convert array
//to string and throw an error. To avoid this error use this :

$newArray = array_unique($array, SORT_REGULAR);
Posted by: Guest on June-15-2021
3

array unique php

<?php
    // Syntax: array_unique(array $array, int $flags = SORT_STRING): array
    // Desc: Removes duplicate values from an array
    $arr = Array("red", "red", "green", "blue", "blue", "white");
    echo "<pre>";
    print_r($arr);
    echo "</pre> <br><br>";
    
    $arrUnique = array_unique($arr);

    echo "<pre>";
    print_r($arrUnique);
    echo "</pre> <br><br>";

    /* -------- output -----------
    Array
    (
        [0] => red
        [1] => red
        [2] => green
        [3] => blue
        [4] => blue
        [5] => white
    )
    
    Array
    (
        [0] => red
        [2] => green
        [3] => blue
        [5] => white
    )
    */
?>
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language