[英]Php array unique comparing 2 types
I have data (more than 10000) from excel, data format looks like, 我有来自excel的数据(超过10000个),数据格式如下:
Example : 范例:
Message | Number
Test 1 021
Test 2 033
Test 3 022
Test 3 022
Test 3 033
Test 2 022
Test 2 021
Input accepted should be 接受的输入应为
same number & different message
different number & same message
different number & different message
I'm trying to make validation, using array, I tried array_unique
, but I can only use 1 type, 我正在尝试使用array进行验证,我尝试了
array_unique
,但是我只能使用1种类型,
$arrayCon[$i]=array("message"=>$message, "number"=>$number);
$realArray=array_unique($arrayCon["number"]);
How do I compare 2 type within 1 array ? 如何在1个数组中比较2个类型?
您需要在数组本身上应用array_unique()
,并指定SORT_REGULAR
标志以比较每个元素:
$realArray = array_unique($arrayCon, SORT_REGULAR)
You can take advantage of JSON and thus make the data row a string, so array_unique
is usable: 您可以利用JSON,从而使数据行成为字符串,因此
array_unique
可以使用:
$source=array();
foreach($database as $row)
{
$source[]=json_encode(array("message"=>$row[0],"number"=>$row[1]));
}
$unique=array_map(function($str){
return json_decode($str,true);
},array_unique($source));
Online demo 在线演示
(I'm behind a proxy right now that seems to mess up with 3v4l.org's result output. If you can't see the result, please let me know) (我现在在代理后面,似乎与3v4l.org的结果输出混淆了。如果看不到结果,请告诉我)
print_r
on my localhost outputs: 我的本地主机输出上的
print_r
:
Array
(
[0] => Array
(
[message] => Test 1
[number] => 021
)
[1] => Array
(
[message] => Test 2
[number] => 033
)
[2] => Array
(
[message] => Test 3
[number] => 022
)
[4] => Array
(
[message] => Test 3
[number] => 033
)
[5] => Array
(
[message] => Test 2
[number] => 022
)
[6] => Array
(
[message] => Test 2
[number] => 021
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.