Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php get unique values by key from array

$array = array(
	array('group' => 'test', 'name' => 'Jane'),
    array('group' => 'test', 'name' => 'John'),
    array('group' => 'admin', 'name' => 'superadmin')
);
$unique_values_by_key = array_unique(array_column($array, 'group'));
Comment

array unique php

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

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

unique key value array php

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

php unique assoc array by value

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

)
  */
?>
Comment

PREVIOUS NEXT
Code Example
Php :: post loop 
Php :: remove last comma from string php foreach 
Php :: laravel swagger install 
Php :: wordpress get post date 
Php :: php path in ubuntu 
Php :: eloquent where raw 
Php :: today date to ago for the date in php 
Php :: laravel eloquent multiple join with where conditions 
Php :: laravel foreach loop index in controller 
Php :: PHP MySQL Use The ORDER BY Clause 
Php :: laravel route optional parameter 
Php :: internal server error phpmyadmin 
Php :: laravel permissions 
Php :: php clear post data 
Php :: laravel 8 websockets 
Php :: php one hour in the future 
Php :: print in file php 
Php :: faker image laravel 8 
Php :: javascript function in php json_encode 
Php :: define php 
Php :: laravel validation numeric vs integer 
Php :: acf sub_field image title 
Php :: export mysql data to word in php 
Php :: php pdo setting error modes 
Php :: laravel simplexmlelement not found 
Php :: laravel collection search 
Php :: drupal 9 custom blocks dependency injection 
Php :: laravel make:middleware 
Php :: php array serialize 
Php :: parse json phph 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =