drpiqlzrh62917192 2016-02-01 17:28
浏览 100
已采纳

PHP数组排列uniq有序

TURNS OUT THAT MY MATH OPERATION WAS THE WRONG ONE! READ PHP array combinations FOR AN EXAMPLE OF COMBINATION


I Am searching the Web now for 3 weeks ( including SO) i dont find someone who has already asked or solved it.

I need a Permutation of an Array in PHP of only uniq ordered values.

$array = array('Number1', 'Number2', 'Number3', 'Number4', 'Number5', 'Number6');

usually a permutation will be like

.1 Number1, Number2, Number3, ...; .2 Number1, Number2, Number4, ...; .3 Number2, Number4, Number1, ...;

But i need it ordered and uniq. so the 3rd line would be ordered to

.1 Number1, Number2, Number3, ...; .2 Number1, Number2, Number4, ...; .3 Number1, Number2, Number4, ...;

And that means Row 3 is NOT uniq and will not be saved in a new array.

2nd Example

array('A','B','C') A AB AC ABC ACB B BC BA BCA BAC C CA CB CAB CBA is wrong for me

I need the output like this

A B C AB BC AC ABC

I kinda solved it so far. BUT my solution is like stoneage and just sorts all new array_permutations and array_filter them. thats like 100 Years calculation time cause 6 numbers permutated with all possabilitys is like X^10 -.-

Please help me guys.

btw: its my first post here, so be gentle :D

  • 写回答

1条回答 默认 最新

  • dotwc62080 2016-02-01 17:57
    关注

    I strongly advise you to take a look the python module called itertools.

    It's a module for efficient looping and constains numerous operations involving permutations, combinatorics and more.

    Despite being a module written in python language it shouldn't difficult to port the code to PHP language. However, since several of these operations may require considerable memory usage is a good idea to use PHP 5.5+, because in this version generators were included.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多