php 数组 任意 排列 组合
和部门同时讨论了一上午了,没有讨论出结果来,基本需求如下:假设:
$a = array('a');
$b = array('b');
$c = array('c');
$d = array('d');
通过一个函数获取以下结果:
array(
a+b+c+d,
ab+c+d,
ac+b+d,
ad+b+c,
bc+ad,
bc+a+d,
bd+ac,
bd+a+c
cd+ab,
cd+a+b,
abc+a,
acd+b,
abd+c,
bcd+a,
...
)
即获取所有数组的组合方式,任意组合数组排序不一定按照顺序,但是要求组合必须出现全部元素。
有高人给个好的算法或者思路么?
回复讨论(解决方案)
用现成的函数实现,未作优化
$a = array('a', 'b', 'c', 'd');$res = array();foreach(arrangement($a) as $v) { $res = array_merge($res, foo(explode(' ', trim($v))));}print_r($res);//排列 arrangementfunction arrangement($arr = array(), $res = '') { if(! is_array($arr) ) $arr = str_split($arr); if(empty($arr)) $array[] = $res; else foreach($arr AS $k => $v) { unset($arr[$k]); foreach( Arrangement($arr, $res . " $v") AS $t) $array[] = $t; $arr[$k] = $v; } return $array;}function foo($ar) { $res = array(); if(count($ar) > 2) { $t = array_shift($ar); foreach(foo($ar) as $v) { $res[] = "$t$v"; $res[] = "$t+$v"; } }else { $res[] = "$ar[0]$ar[1]"; $res[] = "$ar[0]+$ar[1]"; } return $res;}
Array( [0] => abcd [1] => a+bcd [2] => ab+cd [3] => a+b+cd [4] => abc+d [5] => a+bc+d [6] => ab+c+d [7] => a+b+c+d [8] => abdc [9] => a+bdc [10] => ab+dc [11] => a+b+dc [12] => abd+c [13] => a+bd+c [14] => ab+d+c [15] => a+b+d+c [16] => acdb [17] => a+cdb [18] => ac+db [19] => a+c+db [20] => acd+b [21] => a+cd+b [22] => ac+d+b [23] => a+c+d+b [24] => acbd [25] => a+cbd [26] => ac+bd [27] => a+c+bd [28] => acb+d [29] => a+cb+d [30] => ac+b+d [31] => a+c+b+d [32] => adbc [33] => a+dbc [34] => ad+bc [35] => a+d+bc [36] => adb+c [37] => a+db+c [38] => ad+b+c [39] => a+d+b+c [40] => adcb [41] => a+dcb [42] => ad+cb [43] => a+d+cb [44] => adc+b [45] => a+dc+b [46] => ad+c+b [47] => a+d+c+b [48] => bcda [49] => b+cda [50] => bc+da [51] => b+c+da [52] => bcd+a [53] => b+cd+a [54] => bc+d+a [55] => b+c+d+a [56] => bcad [57] => b+cad [58] => bc+ad [59] => b+c+ad [60] => bca+d [61] => b+ca+d [62] => bc+a+d [63] => b+c+a+d [64] => bdac [65] => b+dac [66] => bd+ac [67] => b+d+ac [68] => bda+c [69] => b+da+c [70] => bd+a+c [71] => b+d+a+c [72] => bdca [73] => b+dca [74] => bd+ca [75] => b+d+ca [76] => bdc+a [77] => b+dc+a [78] => bd+c+a [79] => b+d+c+a [80] => bacd [81] => b+acd [82] => ba+cd [83] => b+a+cd [84] => bac+d [85] => b+ac+d [86] => ba+c+d [87] => b+a+c+d [88] => badc [89] => b+adc [90] => ba+dc [91] => b+a+dc [92] => bad+c [93] => b+ad+c [94] => ba+d+c [95] => b+a+d+c [96] => cdab [97] => c+dab [98] => cd+ab [99] => c+d+ab [100] => cda+b [101] => c+da+b [102] => cd+a+b [103] => c+d+a+b [104] => cdba [105] => c+dba [106] => cd+ba [107] => c+d+ba [108] => cdb+a [109] => c+db+a [110] => cd+b+a [111] => c+d+b+a [112] => cabd [113] => c+abd [114] => ca+bd [115] => c+a+bd [116] => cab+d [117] => c+ab+d [118] => ca+b+d [119] => c+a+b+d [120] => cadb [121] => c+adb [122] => ca+db [123] => c+a+db [124] => cad+b [125] => c+ad+b [126] => ca+d+b [127] => c+a+d+b [128] => cbda [129] => c+bda [130] => cb+da [131] => c+b+da [132] => cbd+a [133] => c+bd+a [134] => cb+d+a [135] => c+b+d+a [136] => cbad [137] => c+bad [138] => cb+ad [139] => c+b+ad [140] => cba+d [141] => c+ba+d [142] => cb+a+d [143] => c+b+a+d [144] => dabc [145] => d+abc [146] => da+bc [147] => d+a+bc [148] => dab+c [149] => d+ab+c [150] => da+b+c [151] => d+a+b+c [152] => dacb [153] => d+acb [154] => da+cb [155] => d+a+cb [156] => dac+b [157] => d+ac+b [158] => da+c+b [159] => d+a+c+b [160] => dbca [161] => d+bca [162] => db+ca [163] => d+b+ca [164] => dbc+a [165] => d+bc+a [166] => db+c+a [167] => d+b+c+a [168] => dbac [169] => d+bac [170] => db+ac [171] => d+b+ac [172] => dba+c [173] => d+ba+c [174] => db+a+c [175] => d+b+a+c [176] => dcab [177] => d+cab [178] => dc+ab [179] => d+c+ab [180] => dca+b [181] => d+ca+b [182] => dc+a+b [183] => d+c+a+b [184] => dcba [185] => d+cba [186] => dc+ba [187] => d+c+ba [188] => dcb+a [189] => d+cb+a [190] => dc+b+a [191] => d+c+b+a)
多谢版主,函数我有话下,去重就OK啦。
去重后再次贴出,多谢先,稍后确认。
有重复的吗?显然没有
print_r(array_unique($res));
和
print_r($res);
的结果一样!
var_dump(count($res) == count(array_unique($res)));
bool(true)
抱歉版主,我没有解释清楚,A+B+C+D 和 B+A+C+D, D+C+A+B等算一种。

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),