Answers for "how to get a distinct value from an array in php"

PHP
2

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
0

get unique array from multidimentional array by value in php

$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
Posted by: Guest on January-30-2021

Code answers related to "how to get a distinct value from an array in php"

Browse Popular Code Answers by Language