0

如何显示一次数组元素?例如

var array = ["b","c","c","a","d","e","a","d"]

show => ["a","b","c","d","e"] ?

有什么例子吗?

4

5 回答 5

1

使用该array_unique()功能删除重复项和排序 - http://php.net/manual/en/function.array-unique.php

跟进该array_values()函数将消除数组索引中的空白 - http://php.net/manual/en/function.array-values.php

// initialize array
var $array = array("b","c","c","a","d","e","a","d");
// remove duplicates and sort by string value
$array = array_unique($array, SORT_STRING);
// reindex array (numeric index will have gaps where the duplicates where removed)
$array = array_values($array);
// show results
print_r($array);

在 Javascript/jQuery 中,您可以使用unique()andsort()方法

// initialize array
var array = ["b","c","c","a","d","e","a","d"];
// remove duplicate values
array.unique();
// sort remaining items
array.sort();

如果您在 Javascript 中对数字进行排序,则需要传递一个函数,因为数组默认按字典顺序排序

array.sort(function(a,b){return a - b;});
于 2012-10-22T04:58:47.407 回答
1

PHP

array_unique($array, SORT_STRING)

jQuery

array.unique()
于 2012-10-22T05:00:11.383 回答
0

在 php 中你应该使用array_uniqueandsort来对数组进行排序

$arr = array("b","c","c","a","d","e","a","d");
sort($arr);
print_r(array_unique($arr));
于 2012-10-22T05:00:23.877 回答
0
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
    if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});

来源:堆栈溢出

于 2012-10-22T05:02:40.917 回答
0

问题问:- 如何显示一次数组元素?例如

var 数组 = ["b","c","c","a","d","e","a","d"]

显示 => ["a","b","c","d","e"] ?

有什么例子吗?


答案:- $arr = array("b","c","a","d","e","a","d");

$unique = array_unique($arr);//删除所有重复值

sort($unique);//排序在这里完成

回声“”;print_r($unique);//显示 show => ["a","b","c","d","e"]

于 2012-10-22T07:14:59.213 回答