I have an array :
我有一个数组:
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
Without using any array defualt function, i want to get the count of the array...
不使用任何数组defualt函数,我想得到数组的计数...
Condition:
条件:
Duplicate array values must be considered as 1 value.
必须将重复的数组值视为1值。
Please provide your logic that could help to find my answer without array functions...
请提供您的逻辑,有助于找到我的答案没有数组函数...
Thanks in advance...
提前致谢...
5 个解决方案
#1
5
If you just want to get count of array without duplicates you can do it this way:
如果你只是想获得没有重复的数组,你可以这样做:
<?php
$arrData = array(3, 5, 5, 8, 7, 6, 3, 1, 2, 8, 9);
$out = array();
foreach ($arrData as $item) {
if (!in_array($item, $out)) {
$out[] = $item;
}
}
echo count ($out);
And if you want to know number of occurences you can do it this way:
如果你想知道发生的次数你可以这样做:
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$out = array();
foreach ($arrData as $item) {
if (isset($out[$item])) {
++$out[$item];
}
else {
$out[$item] = 1;
}
}
foreach ($out as $item => $count) {
echo $item.' '.$count."<br />";
}
echo count($out);
You simple iterate over each element using loop and create new array with keys that are values from first array and values that are number of occurrences.
您可以使用循环对每个元素进行简单迭代,并使用第一个数组中的值和出现次数的值创建新数组。
#2
1
Use something like this:
使用这样的东西:
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$tmpArray = array();
$i = 0;
foreach($arrData as $k => $v)
{
if(!in_array($v, $tmpArray))
{
$tmpArray[$k] = $v;
$i++;
}
}
echo "Unique array size is: ".$i;
?>
Without in_array
没有in_array
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$tmpArray = array();
foreach($arrData as $k => $v)
{
$tmpArray[$k] = $v;
}
$i = 0;
foreach($tmpArray as $k => $v)
{
$i++;
}
echo "Unique array size is: ".$i;
?>
#3
1
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
foreach($arrData as $x){
foreach($arrData as $y=>$t){
if($x == $y){
$array[$x] =0;
}else{
$array[$x] =1;
}
}
}
$numCount =0;
foreach($array as $q=>$r){
$numCount +=1;
}
echo $numCount ;
?>
#4
0
Use this php function
使用这个PHP功能
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
array_count_values($arrData );
print_r($arrData );
widtout function default for dublicat array value
widtout函数默认为dublicat数组值
$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);
$i=0;
$valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){
if ($arrData[$key]==$valueArray){
$newDublicatValue[]=$arrData[$key];
}
$valueArray=$arrData[$key];
}
print_r($newDublicatValue);
or use this function for remove dublicate value in array
或使用此函数删除数组中的dublicate值
$result = array_unique($arrData );
print_r($result);
widtout function default for dublicat array value
widtout函数默认为dublicat数组值
$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);
$i=0;
$valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){
if ($arrData[$key]==$valueArray){
unset($arrData[$i]);
}
$valueArray=$arrData[$i];
$i++;
}
print_r($arrData);
#5
0
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1)); // will print unique values
echo "<br>";
echo "Unique value count => ".count(arr_unique($arr1)); // Will print unique value count
echo "<br>";
arr_count_val($arr1); // will print array with duplicate values
function arr_unique($arr) {
sort($arr);
$curr = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
function arr_count_val($arr) {
$uni_array = arr_unique($arr);
$count = 0;
foreach($uni_array as $n1) {
foreach($arr as $n2) {
if($n1 == $n2) {
$count++;
}
}
echo "$n1"." => "."$count"."<br>";
$count=0;
}
}
?>
#1
5
If you just want to get count of array without duplicates you can do it this way:
如果你只是想获得没有重复的数组,你可以这样做:
<?php
$arrData = array(3, 5, 5, 8, 7, 6, 3, 1, 2, 8, 9);
$out = array();
foreach ($arrData as $item) {
if (!in_array($item, $out)) {
$out[] = $item;
}
}
echo count ($out);
And if you want to know number of occurences you can do it this way:
如果你想知道发生的次数你可以这样做:
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$out = array();
foreach ($arrData as $item) {
if (isset($out[$item])) {
++$out[$item];
}
else {
$out[$item] = 1;
}
}
foreach ($out as $item => $count) {
echo $item.' '.$count."<br />";
}
echo count($out);
You simple iterate over each element using loop and create new array with keys that are values from first array and values that are number of occurrences.
您可以使用循环对每个元素进行简单迭代,并使用第一个数组中的值和出现次数的值创建新数组。
#2
1
Use something like this:
使用这样的东西:
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$tmpArray = array();
$i = 0;
foreach($arrData as $k => $v)
{
if(!in_array($v, $tmpArray))
{
$tmpArray[$k] = $v;
$i++;
}
}
echo "Unique array size is: ".$i;
?>
Without in_array
没有in_array
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
$tmpArray = array();
foreach($arrData as $k => $v)
{
$tmpArray[$k] = $v;
}
$i = 0;
foreach($tmpArray as $k => $v)
{
$i++;
}
echo "Unique array size is: ".$i;
?>
#3
1
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
foreach($arrData as $x){
foreach($arrData as $y=>$t){
if($x == $y){
$array[$x] =0;
}else{
$array[$x] =1;
}
}
}
$numCount =0;
foreach($array as $q=>$r){
$numCount +=1;
}
echo $numCount ;
?>
#4
0
Use this php function
使用这个PHP功能
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
array_count_values($arrData );
print_r($arrData );
widtout function default for dublicat array value
widtout函数默认为dublicat数组值
$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);
$i=0;
$valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){
if ($arrData[$key]==$valueArray){
$newDublicatValue[]=$arrData[$key];
}
$valueArray=$arrData[$key];
}
print_r($newDublicatValue);
or use this function for remove dublicate value in array
或使用此函数删除数组中的dublicate值
$result = array_unique($arrData );
print_r($result);
widtout function default for dublicat array value
widtout函数默认为dublicat数组值
$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);
$i=0;
$valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){
if ($arrData[$key]==$valueArray){
unset($arrData[$i]);
}
$valueArray=$arrData[$i];
$i++;
}
print_r($arrData);
#5
0
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1)); // will print unique values
echo "<br>";
echo "Unique value count => ".count(arr_unique($arr1)); // Will print unique value count
echo "<br>";
arr_count_val($arr1); // will print array with duplicate values
function arr_unique($arr) {
sort($arr);
$curr = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
function arr_count_val($arr) {
$uni_array = arr_unique($arr);
$count = 0;
foreach($uni_array as $n1) {
foreach($arr as $n2) {
if($n1 == $n2) {
$count++;
}
}
echo "$n1"." => "."$count"."<br>";
$count=0;
}
}
?>