使用php查找数组中出现一次的元素

Ani*_*nis -1 php arrays

我有很多关于这个问题的答案使用其他语言,但我想要一个 php 语言的答案。请任何人帮助我这是我的阵列看起来像

$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
Run Code Online (Sandbox Code Playgroud)

Ana*_*Die 5

使用array_count_values()如下所示:-

<?php

$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];

$array_count_values = array_count_values($array);// get how many times a value appreas inside array

foreach($array_count_values as $key=>$val){ // now iterate over this newly created array
   if($val ==1){ // if count is 1
     echo $key. " in array come only one time.\n"; // this means value appears only one time inside array
   }
}
Run Code Online (Sandbox Code Playgroud)

输出:- https://eval.in/867433https://eval.in/867434

如果您想要数组中的值:-

<?php

$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11,13]; // increased one value to show you the output

$array_count_values = array_count_values($array);

$single_time_comming_values_array = [];
foreach($array_count_values as $key=>$val){
   if($val ==1){
     $single_time_comming_values_array[] =  $key;
   }
}

print_r($single_time_comming_values_array);
Run Code Online (Sandbox Code Playgroud)

输出:- https://eval.in/867515