나는 단어배열의 중복 수를 계산 하시겠습니까?
(“hello”, “apple”, “hello”, “hello”, “apple”, “orange”, “cake”)
Result here should be 5
다음과 같은 배열의 접촉을 내 배열에 존재 얼마나 많은 중복 단어를 계산하는 데 사용할 수있는 PHP
에서 라이브러리 함수가있는 경우 당신이 말해 주시겠습니까? 어떤 도움이라도 대단히 감사하겠습니다.
나는 단어배열의 중복 수를 계산 하시겠습니까?
(“hello”, “apple”, “hello”, “hello”, “apple”, “orange”, “cake”)
Result here should be 5
다음과 같은 배열의 접촉을 내 배열에 존재 얼마나 많은 중복 단어를 계산하는 데 사용할 수있는 PHP
에서 라이브러리 함수가있는 경우 당신이 말해 주시겠습니까? 어떤 도움이라도 대단히 감사하겠습니다.
$array = array(“hello”, “apple”, “hello”, “hello”, “apple”, “orange”, “cake”);
$unique_elements = array_unique($array);
$totalUniqueElements = count($unique_elements);
echo $totalUniqueElements;
//Output 5
Hope this will help you.
@nickb 내 텍스트 형식을 개선해 주셔서 감사합니다. –
당신은 count()
와 array_unique()
을 결합 할 수 있습니다 :
$number_of_duplicates = count($words) - count(array_unique($words));
참고 : PHP는 백 Array Functions에 걸쳐있다. 그들을 배우면 better PHP Developer이됩니다.
당신이 할 수있는이
$count1 = count($array);
$count2 = count(array_unique($array));
echo $count1 - $count2;
미안하지만 나는 그가 유일한 카운트가 필요하다고 생각했다 .... 내 ans을 업데이트했다. – Gautam3164
같은 시도 :
$org = count($array);
$unique = count(array_unique($array));
$duplicates = $org - $unique
확인 http://us2.php.net/manual/en/function.array-count-values.php
<?php
var_dump(array_count_values($words));
Output:
Array(
[hello] => 3,
[apple] => 2,
[orange] => 1,
[cake] => 1
)
이것은 직접적으로 대답하지는 않지만, 대안적인 접근법이다. –
array_count_values
가 detaied 정보를 여러분 모두 감사합니다. 그들 모두는 매우 도움이되었다 –