“array php unik” Kode Jawaban

array_unique


<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

Array
(
    [a] => green
    [0] => red
    [1] => blue
)
Alberto Peripolli

array php unik

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Denis Chupin

array php unik

<?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
    )
    */
?>

PHP unik asosiatif array berdasarkan nilai

<?php
    function uniquAsoc($array,$key){
        $resArray=[];
        foreach($array as $val){
          if(empty($resArray)){
            array_push($resArray,$val);
          }else{
            $value=array_column($resArray,$key);
            if(!in_array($val[$key],$value)){
                array_push($resArray,$val);
              }
          }          
        }
        
        return $resArray;
    }
$array=[['phone'=>123,'id'=>1],['phone'=>748,'id'=>1],['phone'=>958,'id'=>3]];
print_r(uniquAsoc($array,'id')); 
/*
Array
(
    [0] => Array
        (
            [phone] => 123
            [id] => 1
        )

    [1] => Array
        (
            [phone] => 958
            [id] => 3
        )

)
  */
?>
WebApi

Periksa data duplikat di array php

$counts = array_count_values($array);
            $duplicate_title  = array_filter($array, function ($value) use ($counts) {
                return $counts[$value] > 1;
            });
Easy Elephant

PHP array nilai kunci unik

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Lokesh003Coding

Jawaban yang mirip dengan “array php unik”

Pertanyaan yang mirip dengan “array php unik”

Lebih banyak jawaban terkait untuk “array php unik” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya