[英]count values in a php array and eliminate repeated
I'm trying to count the values in a php array and eliminate repetitions have leaving only 1. The format of my arrangement is as follows: 我正在尝试计算php数组中的值,并消除仅保留1的重复。我的排列格式如下:
Array
(
[0] => Array
(
[inscripcion_id] => 3932
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[1] => Array
(
[inscripcion_id] => 3926
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[2] => Array
(
[inscripcion_id] => 3921
[nombre] => Nicolas
[email] => nico@prueba.com
)
)
I look matches are based on the email
field, if the email appears more than 1 time in the arrangements must be eliminated leaving only 1. The problem is I can not get as often appears no less eliminate it. 我看匹配项基于email
字段,如果在安排中消除出现1次以上的email
,则必须只保留1条。问题是我无法像平常一样出现,因此消除不了。 Try array_count_values
does not work, I can find the matches of each email but only in the subarray therefore always find 1 each as follows for example: 尝试array_count_values
不起作用,我可以找到每封电子邮件的匹配项,但只能在子array_count_values
中找到,因此总能找到1条,例如:
Array ( [3932] => 1 [Prueba] => 1 [prueba@prueba.cl] => 1 )
Array ( [3926] => 1 [Prueba] => 1 [prueba@prueba.cl] => 1 )
I hope the result is this: 我希望结果是这样的:
Array
(
[0] => Array
(
[inscripcion_id] => 3932
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[1] => Array
(
[inscripcion_id] => 3921
[nombre] => Nicolas
[email] => nico@prueba.com
)
)
This Should do it. 这应该做。
print_r(unique_multidim_array($array, 'email'));
function unique_multidim_array($array, $key){
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val){
if(!in_array($val[$key],$key_array)){
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
Create an array with the email fields and remove from source array if duplicate: 使用电子邮件字段创建一个数组,如果重复则从源数组中删除:
$temp = array();
$out = array();
foreach( $array as $k => $v ) {
if ( isset( $temp[$v['email']] ) )
unset( $array[$k]);
else {
$temp[$v['email']] = $k;
array_push( $out, $v );
}
}
The original array is in $array
and the output in $out
原始数组在$array
,输出在$out
If the key is not important an easier way is 如果密钥不重要,则更简单的方法是
$temp = array();
foreach( $array as $k => $v ) {
if ( isset( $temp[$v['email']] ) )
unset( $array[$k]);
else
$temp[$v['email']] = $k;
}
You can try this to make an array index by the email
(like a primary key). 您可以尝试通过email
(如主键)来建立数组索引。
$data = array(
array(
'inscripcion_id'=>3932,
'nombre'=>'Prueba',
'email'=>'prueba@prueba.cl'
),
array(
'inscripcion_id'=>3926,
'nombre'=>'Prueba',
'email'=>'prueba@prueba.cl'
),
array(
'inscripcion_id'=>3921,
'nombre'=>'Nicolas',
'email'=>'nico@prueba.com'
),
);
$result = array();
foreach($data as $key => $value) {
if (!in_array($value['email'], $result)) {
$result[$value['email']] = $value;
}
}
var_dump($result);
Result: 结果:
array (size=2)
'prueba@prueba.cl' =>
array (size=3)
'inscripcion_id' => int 3926
'nombre' => string 'Prueba' (length=6)
'email' => string 'prueba@prueba.cl' (length=16)
'nico@prueba.com' =>
array (size=3)
'inscripcion_id' => int 3921
'nombre' => string 'Nicolas' (length=7)
'email' => string 'nico@prueba.com' (length=15)
Also you can have direct access to every sub array you want using it's email value: 同样,您可以使用其电子邮件值直接访问您想要的每个子数组:
var_dump($result['prueba@prueba.cl']);
Result: 结果:
array (size=3)
'inscripcion_id' => int 3926
'nombre' => string 'Prueba' (length=6)
'email' => string 'prueba@prueba.cl' (length=16)
Just create the result array with the email as the key, then there are no duplicates: 只需使用电子邮件作为键创建结果数组,那么就不会重复:
foreach($array as $value) {
$result[$value['email']] = $value;
}
Then if you want to reset the keys: 然后,如果您想重置密钥:
$result = array_values($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.