An algorithm for combining two-dimensional array elements

Source: Internet
Author: User
$arr = Array ('    a ', ' B ', ' C '), Array (' C ', ' F '), Array ('    g ', ' Z '), array (    ' x ', ' Y '));//$ Arr subset element length may be more//$arr subset element with $arr other subset element 22 combination or 3344 combination//subset array (' A ', ' B ', ' C ') element does not need combination//22 combination $newarr = Array (  Array (' A ', ' C '), Array (' A ', ' F '), array (' B ', ' C '), Array (' B ', ' F '), Array (' C  ',  ' C '),  array (' C ', ' F '),  ...) 33 Combination $newarr = Array (  array (' A ', ' C ', ' G '),  array (' A ', ' f ', ' G '),  array (' B ', ' C ', ' G '),  array (' B ' , ' f ', ' G '),  array (' C ', ' C ', ' G '),  array (' C ', ' f ', ' G '),  ...) 44 Combination $newarr = Array (  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 it be implemented with a function?
$arr: Original Array, $cNum: Combined length function getcombination ($arr, $cNum) {  ...}


Reply to discussion (solution)

You rub together a combination (n takes M's combination) and a Descartes (Cartesian product) function.
In fact, there is no need to write a function, write a rather lost universality

I was thinking first of all, to take an array of length, and then to ask for Cartesian product
First, the recursive, then the product, the final merge array?

If this is the case, then

function Getcombination ($arr, $cNum) {   $res = array ();   foreach (Combination ($arr, $cNum) as $item) {      $res = Array_merge ($res, Descartes ($item));   }   return $res;}
You can do it.

The possible notation for two functions is
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, 2), $len, ' 0 ', STR_PA D_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); fo Reach ($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. Call when you need to, don't write every time

Common algorithms should be written as functions to form a code base. Call when you need to, don't write every time

Thank you moderator, sleep, immediately read your writing, study and study
  • 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.