获得所有可能的组合而无重复

编程入门 行业动态 更新时间:2024-10-09 07:17:46
本文介绍了获得所有可能的组合而无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获得给定数字的所有可能组合。 例如,我有

How can I get all the possible combinations of given numbers. For instance I have

$ arr = [1,2,3,4]

我想获取组合内没有任何重复的组合

I want to get the combinations without any duplicates inside the combinations

[1] => [1] [2] => [2] [3] => [3] [4] => [4] [5] => [1, 2] [6] => [1, 3] [7] => [1, 4] [8] => [2, 3] [9] => [2, 4] [10] => [3, 4] [11] => [1, 2, 3] [12] => [1, 2, 4] [13] => [1, 3, 4] [14] => [2, 3, 4] [15] => [1, 2, 3, 4]

推荐答案

我希望下面的函数按照您的预期输出工作:

I hope below function work as per your expected output :

function get_array_combination($arr) { $results = array(array( )); foreach ($arr as $values) foreach ($results as $combination) array_push($results, array_merge($combination, array($values))); // Get new values and merge to your previous combination. And push it to your results array return $results; } $set = array('1', '2', '3', '4'); $final_array = get_array_combination($set); echo "<pre>"; print_r(array_values(array_filter($final_array))); // Removed blank entry from array and re indexing array keys.

更多推荐

获得所有可能的组合而无重复

本文发布于:2023-11-30 12:35:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   而无

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!