简体   繁体   English

获取数组中的唯一值并计数而不使用数组函数

[英]Get unique values in array and count without array functions

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... 不使用任何数组默认功能,我想获取数组的数量...

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... 提前致谢...

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. 您可以使用循环对每个元素进行简单的迭代,并使用键(来自第一个数组的值和出现次数的值)创建新数组。

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

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 或使用此函数删除数组中的双精度值

$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);
<?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; 
      }
  }
?>

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

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