How to deal with this change, php array combination

Source: Internet
Author: User
Please help me. how can I deal with this change? php array combination. for example, I have two arrays.
Group A: 10, 11, 12, 13
Group B: 14,15

To be combined into 10 | 15

Thanks. three or four groups may occur, for example, Group A, Group B, Group C, and Group D.


Reply to discussion (solution)

You are seeking Cartesian product. There are several discussions in the essence area.
Here we will give another version.

$a = array(10,11,12,13);$b = array(14,15);foreach(Descartes($a, $b) as $v) $r[] = join(',', $v);echo join('|', $r);
|

  function Descartes($d) {if(func_num_args() > 1) $d = func_get_args();$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;  }

You are seeking Cartesian product. There are several discussions in the essence area.
Here we will give another version.

$a = array(10,11,12,13);$b = array(14,15);foreach(Descartes($a, $b) as $v) $r[] = join(',', $v);echo join('|', $r);
|

  function Descartes($d) {if(func_num_args() > 1) $d = func_get_args();$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;  }


Thank you, moderator. I will test it later. I will release another requirement later. I will help you later.

Http://bbs.csdn.net/topics/391860657 God help me look at this

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.