Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从一个数组中获取所有可能的键组合_Php_Arrays_Combinations - Fatal编程技术网

php从一个数组中获取所有可能的键组合

php从一个数组中获取所有可能的键组合,php,arrays,combinations,Php,Arrays,Combinations,我有一个数组:(数组中的值总是偶数,但大于8,数组值的一半也是偶数) 我想要的结果是: array( [0] => array(apple => "banana", pear => "grape", cherry => "orange", pineapple => "kiwi"), [1] => array(grape => "apple", banana => "pear", orange => "pineapple", kiwi =>

我有一个数组:(数组中的值总是偶数,但大于8,数组值的一半也是偶数)

我想要的结果是:

array(
[0] => array(apple => "banana", pear => "grape", cherry => "orange", pineapple => "kiwi"),
[1] => array(grape => "apple", banana => "pear", orange => "pineapple", kiwi => "cherry"),
...etc
[7] => array(banana => "apple", grape => "pear", orange => "cherry", kiwi => "pineapple"),
[8] => array(apple => "grape", pear => "banana", pineapple => "orange", cherry => "kiwi"), 
...etc until [13]
)
正如您所看到的,每个值有时是一个键,有时是一个值,除了相同的值之外,所有可能的组合都必须进行(苹果和苹果不能满足)

如果第一次遭遇[0]“apple”是一个键,那么在下一次遭遇[1]中它应该是一个值

相同的两个值(apple和banana)在数组的结束部分之前不能再次相遇


有人能帮我吗?

多亏了谷歌,我才找到了答案

对我的问题最恰当的参考是运动队比赛。
在谷歌上搜索,我找到了答案。

我只找到了问题的一部分,我最感兴趣的部分是
如果第一次遇到[0]“苹果”是一个键,在下一次遇到[1]它应该是一个值,两个值相同(苹果和香蕉)在数组的最后一半之前无法再次相遇。
我认为可以有64种可能的组合。实际上不,因为在apple与banana相遇的同一数组结果值中,其他键也会相遇,计算所有键+值组合是的,如果apple与apple相遇,可能会有64-8或64,但只计算主数组键,结果应该包含14个键,现在我说我注意到我已经写了15个。它应该从0到13。
array(
[0] => array(apple => "banana", pear => "grape", cherry => "orange", pineapple => "kiwi"),
[1] => array(grape => "apple", banana => "pear", orange => "pineapple", kiwi => "cherry"),
...etc
[7] => array(banana => "apple", grape => "pear", orange => "cherry", kiwi => "pineapple"),
[8] => array(apple => "grape", pear => "banana", pineapple => "orange", cherry => "kiwi"), 
...etc until [13]
)