简体   繁体   English

需要获取数组中的唯一值

[英]Need to get unique values in array

Array
(
)
Array
(
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
    [1] => 12
)

I want this array 我想要这个数组

Array
(
    [0] => 14
    [1] => 12
)

Here is my code: 这是我的代码:

$colorarray = array();
foreach($catIds as $catid){
    $colorarray[] = $catid;
}

Need to get unique array values 需要获取唯一的数组值

Thanks 谢谢

You can use call_user_func_array with array-merge for flatten your array and then use array-unique as: 您可以将call_user_func_arrayarray-merge结合使用来展平阵列,然后将array-unique用作:

$res = call_user_func_array('array_merge', $arr);
print_r(array_unique($res)); // will give 12 and 14

Live example 3v4l 直播示例3v4l

Or as @Progrock suggested: $output = array_unique(array_merge(...$data)); 或者按照@Progrock的建议: $output = array_unique(array_merge(...$data)); (I like that syntax using the ... ) (我喜欢使用...语法)

You can always do something like this: 您始终可以执行以下操作:

$colorarray = array();
foreach($catIds as $catid){
    if(!in_array($catid, $colorarray) {
       $colorarray[] = $catid;
    }
}

But also this has n*n complexity, So if your array is way too big, it might not be the most optimised solution for you. 但这也具有n * n的复杂性,因此,如果您的数组太大,那么它可能不是您最优化的解决方案。

You can do following to generate unique array. 您可以执行以下操作以生成唯一数组。

array_unique($YOUR_ARRAY_VARIABLE, SORT_REGULAR);

this way only unique value is there in your array instead of duplication. 这样,数组中只有唯一值,而不是重复值。

UPDATED 更新

This is also one way to do same 这也是做同样的一种方法

<?php
 // define array 
 $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5); 

 // print original array 
 echo "Original Array : \n"; 
 print_r($a); 

 // remove duplicate values by using  
 // flipping keys and values 
 $a = array_flip($a); 

 // restore the array elements by again  
 // flipping keys and values. 
 $a = array_flip($a); 

 // re-order the array keys 
 $a= array_values($a); 

 // print updated array 
 echo "\nUpdated Array : \n "; 
 print_r($a); 
?>

Reference link 参考链接

Hope this will helps you 希望这对您有帮助

PHP: Removes duplicate values from an array PHP:从数组中删除重复的值

<?php
$fruits_list = array('Orange',  'Apple', ' Banana', 'Cherry', ' Banana');
$result = array_unique($fruits_list);
print_r($result);
?>

------your case-------- ------您的案子--------

  $result = array_unique($catIds);
    print_r($result);

You can construct a new array of all values by looping through each sub-array of the original, and then filter the result with array_unique: 您可以通过遍历原始对象的每个子数组来构造一个包含所有值的新数组,然后使用array_unique过滤结果:

<?php
$data =
[
    [
        0=>13
    ],
    [
        0=>13
    ],
    [
        0=>17,
        1=>19
    ]
];

foreach($data as $array)
    foreach($array as $v)
        $all_values[] = $v;

var_export($all_values);
$unique = array_unique($all_values);
var_export($unique);

Output: 输出:

array (
    0 => 13,
    1 => 13,
    2 => 17,
    3 => 19,
  )array (
    0 => 13,
    2 => 17,
    3 => 19,
  )

I have updated your code please check 我已更新您的代码,请检查

$colorarray = array();
foreach($catIds as $catid){
    $colorarray[$catid] = $catid;
}

This will give you 100% unique values. 这将为您提供100%的唯一值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM