PHP 清理置换数组
嘿大家,基本上,我有一个数组:
array('a', 'b', 'c');
现在我通过数组排列函数运行它,结果是:
Array
(
[0] => Array
(
[0] => C
)
[1] => Array
(
[0] => B
)
[2] => Array
(
[0] => B
[1] => C
)
[3] => Array
(
[0] => C
[1] => B
)
[4] => Array
(
[0] => A
)
[5] => Array
(
[0] => A
[1] => C
)
[6] => Array
(
[0] => C
[1] => A
)
[7] => Array
(
[0] => A
[1] => B
)
[8] => Array
(
[0] => B
[1] => A
)
[9] => Array
(
[0] => A
[1] => B
[2] => C
)
[10] => Array
(
[0] => A
[1] => C
[2] => B
)
[11] => Array
(
[0] => B
[1] => A
[2] => C
)
[12] => Array
(
[0] => B
[1] => C
[2] => A
)
[13] => Array
(
[0] => C
[1] => A
[2] => B
)
[14] => Array
(
[0] => C
[1] => B
[2] => A
)
)
现在我的问题是,我如何清理该数组,以便:
array ( C, B )
is the same as
array ( B, C )
并且它删除了第二个数组
我该怎么做?
编辑...根据您的答案进行一些研究后,这就是我得出的结论:
array_walk($array, 'sort');
$array = array_unique($array);
sort($array); // not necessary
Hey all, basically, i have an array:
array('a', 'b', 'c');
Now i run it through an array permutation function and the result is:
Array
(
[0] => Array
(
[0] => C
)
[1] => Array
(
[0] => B
)
[2] => Array
(
[0] => B
[1] => C
)
[3] => Array
(
[0] => C
[1] => B
)
[4] => Array
(
[0] => A
)
[5] => Array
(
[0] => A
[1] => C
)
[6] => Array
(
[0] => C
[1] => A
)
[7] => Array
(
[0] => A
[1] => B
)
[8] => Array
(
[0] => B
[1] => A
)
[9] => Array
(
[0] => A
[1] => B
[2] => C
)
[10] => Array
(
[0] => A
[1] => C
[2] => B
)
[11] => Array
(
[0] => B
[1] => A
[2] => C
)
[12] => Array
(
[0] => B
[1] => C
[2] => A
)
[13] => Array
(
[0] => C
[1] => A
[2] => B
)
[14] => Array
(
[0] => C
[1] => B
[2] => A
)
)
Now my question is, how can i clean that array up so that:
array ( C, B )
is the same as
array ( B, C )
and it removes the second array
How would i do that?
EDIT... after some research based on your answers, this is what I came up with:
array_walk($array, 'sort');
$array = array_unique($array);
sort($array); // not necessary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需 排序 组成数组:
所以 {"C", "B"}变成=> {“B”,“C”}
并且 {"B", "C"} 变为 => {“B”,“C”}
相同。
Just sort the constituent arrays:
So {"C", "B"} becomes => {"B", "C"}
and {"B", "C"} becomes => {"B", "C"}
which are identical.
您还可以使用 pear 包 Math_Combinatorics。
印刷
You can also use the pear package Math_Combinatorics.
prints