A two-dimensional array element combination algorithm

Source: Internet
Author: User
A two-dimensional array element combination algorithm
$ Arr = array ('A', 'B', 'C'), array ('C', 'F'), array ('G ', 'Z'), array ('X', 'y ')); /// the length of the $ arr subset element may be more. // combine the subset element of $ arr with the other subset elements of $ arr, or combine them with three, three, four, and four. // The subset array ('A', elements in 'B', 'C') do not need to be combined // two or two combinations $ newarr = array ('A', 'C'), array ('A ', 'F'), array ('B', 'c'), array ('B', 'F'), array ('C', 'C '), array ('C', 'F '),......) // Three combinations $ newarr = array ('A', 'C', 'g'), array ('A', 'F', 'g '), array ('B', 'c', 'g'), array ('B', 'F', 'g'), array ('C', 'C ', 'G'), array ('C', 'F', 'g '),......) // Four combinations $ newarr = array ('A', 'C', 'G', 'x'), array ('A', 'F ', 'G', 'x'), array ('B', 'c', 'G', 'x'), array ('B', 'F ', 'G', 'x'), array ('C', 'C', 'G', 'x'), array ('C', 'F ', 'G', 'x '),......)

Can I use a function?
// $ Arr: Original array, $ cNum: combination length function getCombination ($ arr, $ cNum ){......}


Reply to discussion (solution)

You can combine a Combination (a Combination of n and m) and a Descartes (Cartesian product) function.
In fact, you don't need to write only one function. writing one function loses its universality.

In the beginning, I also thought that we should first obtain an array that conforms to the length and then calculate the Cartesian product.
First recursion, then product, and finally merge the array?

If so, then

function getCombination($arr, $cNum) {   $res = array();   foreach( Combination($arr, $cNum) as $item) {      $res = array_merge($res, Descartes($item));   }   return $res;}
You can.

Two functions may be written as follows:
function Combination( $arr, $num=0) {$arr = array_values($arr);$len = count($arr);if($num <= 0 || $num > $len) $num = $len;$res = array();for($i=1,$n=pow(2, $len); $i<$n; ++$i) {$tmp = str_pad(base_convert($i, 10, 2), $len, '0', STR_PAD_LEFT);$t = array();for($j=0; $j<$len; ++$j) {if($tmp{$j} == '1') {$t[] = $arr[$j];}}if(count($t) == $num) $res[] = $t;}return $res;}function Descartes($d) {$r = array_pop($d);while($d) {$t = array();$s = array_pop($d);if(! is_array($s)) $s = array($s);foreach($s as $x) {foreach($r as $y) $t[] = array_merge(array($x), is_array($y) ? $y : array($y));}$r = $t;}return $r;}

Common algorithms should be written as functions to form a code base. You do not need to write it every time when you need it.

Common algorithms should be written as functions to form a code base. You do not need to write it every time when you need it.

Thank you, moderator. go to bed. you'll be right away. learn and study again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.