简体   繁体   中英

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.

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

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

$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

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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