数组算法解决思路
程序员文章站
2022-04-18 19:53:38
...
数组算法
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 3
[1] => 2
)
[5] => Array
(
[0] => 2
[1] => 2
)
[6] => Array
(
[0] => 2
[1] => 2
)
[7] => Array
(
[0] => 2
[1] => 2
)
[8] => Array
(
[0] => 2
[1] => 2
)
[9] => Array
(
[0] => 3
[1] => 2
)
[10] => Array
(
[0] => 2
[1] => 3
)
[11] => Array
(
[0] => 20
[1] => 30
)
[12] => Array
(
[0] => 35
[1] => 25
)
[13] => Array
(
[0] => 100
[1] => 15
)
[14] => Array
(
[0] => 2
[1] => 1
)
)
目的:里面有重复的只保留一个,然后数据是全部都是唯一的。倒过来的也认为是重复的,譬如1,2和2,1也当作是重复的。。
自己写了个算法。觉得效率太慢了。。大家还有好的算法不?
------解决方案--------------------
array_unique()
------解决方案--------------------
foreach($inputarray as $value) {
foreach($value as $key=>$v) {
$newarray[$v] = $key;
}
}
$array = array_keys($newarray);
print_r($array);
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 3
[1] => 2
)
[5] => Array
(
[0] => 2
[1] => 2
)
[6] => Array
(
[0] => 2
[1] => 2
)
[7] => Array
(
[0] => 2
[1] => 2
)
[8] => Array
(
[0] => 2
[1] => 2
)
[9] => Array
(
[0] => 3
[1] => 2
)
[10] => Array
(
[0] => 2
[1] => 3
)
[11] => Array
(
[0] => 20
[1] => 30
)
[12] => Array
(
[0] => 35
[1] => 25
)
[13] => Array
(
[0] => 100
[1] => 15
)
[14] => Array
(
[0] => 2
[1] => 1
)
)
目的:里面有重复的只保留一个,然后数据是全部都是唯一的。倒过来的也认为是重复的,譬如1,2和2,1也当作是重复的。。
自己写了个算法。觉得效率太慢了。。大家还有好的算法不?
------解决方案--------------------
array_unique()
------解决方案--------------------
foreach($inputarray as $value) {
foreach($value as $key=>$v) {
$newarray[$v] = $key;
}
}
$array = array_keys($newarray);
print_r($array);
相关文章
相关视频