Array merging and counting the number of elements in the new group

Source: Internet
Author: User
Array merging and counting the number of elements in the new group
$ OldArr = array ('A', 'B', 'C', 'D'), array ('A', 'B ')); // form a new two-dimensional array. The style is as follows: $ newArray = array ('A', 'A'), array ('A', 'B '), array ('B', 'A'), array ('B', 'B '),....... array ('D', 'B'); // $ the number of oldArry sub-elements is unknown. a new two-dimensional array needs to be formed and the arrays with the same elements are removed, for example: array ('A', 'A'), array ('B', 'B ');


I thought about it for one night, but I don't know how to do it. I have to upgrade it.

If $ oldArr has three elements, the number of child elements of $ newarray is also three:

$ OldArr = array ('A', 'B', 'C', 'D'), array ('A', 'B '), array ('e', 'F', 'g'); $ newArray = array ('A', 'A', 'E '), array ('A', 'A', 'F'), array ('A', 'A', 'F'), array ('A', 'B ', 'E '),....... array ('D', 'B', 'g'); // to remove arrays with the same elements, only three different elements can be left, for example: array ('A', 'A', 'E'), array ('A', 'A', 'F ')


Reply to discussion (solution)

Your basic algorithm is to evaluate Cartesian products.
The product of Descartes is also called the straight product. Let A and B be any two sets. in set A, take an element x at will, and take an element y in set B to form an ordered pair (x, y ), take such an ordered pair as A new element. A set of all of them is called the direct product of set A and set B. It is recorded as A × B, that is, A × B = {(x, y) | x, A, and y, B }.
There are a lot of discussions about Cartesian products in the essence area. you can check them out.

In view of your requirements for filtering the result array, you can use a callback function to implement filtering.

$oldArr = array(array('a','b','c','d'),array('a','b'));//$oldArr = array(array('a','b','c','d'),array('a','b'),array('e','f','g'));print_r(Descartes($oldArr, 'foo'));function foo($a) {  return count($a) == count(array_count_values($a));}function Descartes($d, $func='') {  $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) {        $m = array_merge(array($x), is_array($y) ? $y : array($y));        if($func && $func($m)) $t[] = $m;      }    }    $r = $t;  }  return $r;}
Array(    [0] => Array        (            [0] => a            [1] => b        )    [1] => Array        (            [0] => b            [1] => a        )    [2] => Array        (            [0] => c            [1] => a        )    [3] => Array        (            [0] => c            [1] => b        )    [4] => Array        (            [0] => d            [1] => a        )    [5] => Array        (            [0] => d            [1] => b        ))

Vertices x, right, is the Cartesian product.

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.