Combining multiple arrays

Source: Internet
Author: User
Combine and discuss multiple arrays

Suppose there are three arrays (actually not sure how many arrays ):
$ Arr1 = array (1, 2, 3, 4 );
$ Arr2 = array (10, 20, 30, 40 );
$ Arr3 = array (100,200,300,400 );

Result to be implemented:
Array (
Array (100 ),
Array (200 ),
Array (300 ),
Array (400 ),
Array (100 ),
Array (200 ),
Array (300 ),
Array (400 ),
Array (1, 30, 100 ),
...
);
You don't want to use recursion, but you can also implement it.


Reply to discussion (solution)

This post was last edited by xuzuning at 07:58:13

$a = [1, 2];$b = ['A', 'B', 'C'];$c = ['a', 'b'];$d = Descartes($a, $b, $c);print_r($d);function Descartes() {  $d = func_get_args();  if( count($d) < 2 ) return current($d);  $n = array_product( array_map('count', $d) );  array_walk( $d, function(&$v, $k, $n) {      $v = array_chunk( call_user_func_array('array_merge', array_pad([$v], $n/count($v), $v)), 1);      if($k==0) sort($v);    }, $n);  array_unshift($d, 'array_merge');  $d = call_user_func_array('array_map', $d);  array_multisort(array_map(function($v) { return $v[0]; }, $d), $d);  return $d;}
Array(    [0] => Array        (            [0] => 1            [1] => A            [2] => a        )    [1] => Array        (            [0] => 1            [1] => A            [2] => b        )    [2] => Array        (            [0] => 1            [1] => B            [2] => a        )    [3] => Array        (            [0] => 1            [1] => B            [2] => b        )    [4] => Array        (            [0] => 1            [1] => C            [2] => a        )    [5] => Array        (            [0] => 1            [1] => C            [2] => b        )    [6] => Array        (            [0] => 2            [1] => A            [2] => a        )    [7] => Array        (            [0] => 2            [1] => A            [2] => b        )    [8] => Array        (            [0] => 2            [1] => B            [2] => a        )    [9] => Array        (            [0] => 2            [1] => B            [2] => b        )    [10] => Array        (            [0] => 2            [1] => C            [2] => a        )    [11] => Array        (            [0] => 2            [1] => C            [2] => b        ))

Well, this is correct. # Some problems with 1. it is incorrect to input four arrays.

$a = [1, 2];$b = ['A', 'B', 'C'];$c = ['a', 'b'];$d = ['D', 'E', 'F'];$r = Descartes($a, $b, $c);$r = Descartes($a, $b, $c, $d);print_r( array_map(function($v) { return join(',', $v); }, $r) );function Descartes() {  $d = func_get_args();  if( count($d) < 2 ) return current($d);  $n = array_product( array_map('count', $d) );  array_walk( $d, function(&$v, $k, $n) {      $v = array_chunk( call_user_func_array('array_merge', array_pad([$v], $n/count($v), $v)), 1);    }, $n);  foreach($d as $i=>$t) {    if($i == 0) {      $r = $t;    }else {      array_multisort(array_map(function($v) { return $v[0]; }, $r), $r);      $r = array_map('array_merge', $r, $t);    }  }  return $r;}
Array(    [0] => 1,A,a,D    [1] => 1,A,a,E    [2] => 1,A,a,F    [3] => 1,A,b,D    [4] => 1,A,b,E    [5] => 1,A,b,F    [6] => 1,B,a,D    [7] => 1,B,a,E    [8] => 1,B,a,F    [9] => 1,B,b,D    [10] => 1,B,b,E    [11] => 1,B,b,F    [12] => 1,C,a,D    [13] => 1,C,a,E    [14] => 1,C,a,F    [15] => 1,C,b,D    [16] => 1,C,b,E    [17] => 1,C,b,F    [18] => 2,A,a,D    [19] => 2,A,a,E    [20] => 2,A,a,F    [21] => 2,A,b,D    [22] => 2,A,b,E    [23] => 2,A,b,F    [24] => 2,B,a,D    [25] => 2,B,a,E    [26] => 2,B,a,F    [27] => 2,B,b,D    [28] => 2,B,b,E    [29] => 2,B,b,F    [30] => 2,C,a,D    [31] => 2,C,a,E    [32] => 2,C,a,F    [33] => 2,C,b,D    [34] => 2,C,b,E    [35] => 2,C,b,F)

Well, this is correct. # Some problems with 1. it is incorrect to input four arrays.

$a = [1, 2];$b = ['A', 'B', 'C'];$c = ['a', 'b'];$d = ['D', 'E', 'F'];$r = Descartes($a, $b, $c);$r = Descartes($a, $b, $c, $d);print_r( array_map(function($v) { return join(',', $v); }, $r) );function Descartes() {  $d = func_get_args();  if( count($d) < 2 ) return current($d);  $n = array_product( array_map('count', $d) );  array_walk( $d, function(&$v, $k, $n) {      $v = array_chunk( call_user_func_array('array_merge', array_pad([$v], $n/count($v), $v)), 1);    }, $n);  foreach($d as $i=>$t) {    if($i == 0) {      $r = $t;    }else {      array_multisort(array_map(function($v) { return $v[0]; }, $r), $r);      $r = array_map('array_merge', $r, $t);    }  }  return $r;}
Array(    [0] => 1,A,a,D    [1] => 1,A,a,E    [2] => 1,A,a,F    [3] => 1,A,b,D    [4] => 1,A,b,E    [5] => 1,A,b,F    [6] => 1,B,a,D    [7] => 1,B,a,E    [8] => 1,B,a,F    [9] => 1,B,b,D    [10] => 1,B,b,E    [11] => 1,B,b,F    [12] => 1,C,a,D    [13] => 1,C,a,E    [14] => 1,C,a,F    [15] => 1,C,b,D    [16] => 1,C,b,E    [17] => 1,C,b,F    [18] => 2,A,a,D    [19] => 2,A,a,E    [20] => 2,A,a,F    [21] => 2,A,b,D    [22] => 2,A,b,E    [23] => 2,A,b,F    [24] => 2,B,a,D    [25] => 2,B,a,E    [26] => 2,B,a,F    [27] => 2,B,b,D    [28] => 2,B,b,E    [29] => 2,B,b,F    [30] => 2,C,a,D    [31] => 2,C,a,E    [32] => 2,C,a,F    [33] => 2,C,b,D    [34] => 2,C,b,E    [35] => 2,C,b,F)


I was a little dizzy. I had to look at it slowly. I used a lot of functions that were rarely used before. but what is the method of brackets? 5.3.12 not supported
array_pad([$v], $n/count($v), $v)

Can array () be simplified to [] php5.4? How can I remember php5.3?

Simple comments:

$ D = func_get_args (); // read the input parameters to the array (php4) if (count ($ d) <2) return current ($ d ); $ n = array_product (array_map ('count', $ d); // array_product calculates the product of the array value (php5). array_map returns the array (php4) processed by the callback function) array_walk ($ d, function (& $ v, $ k, $ n) {// use the callback function to process the closure of array elements (php3) (php5.3) $ v = array_chunk (call_user_func_array ('Array _ merge', array_pad ([$ v], $ n/count ($ v), $ v), 1 );}, $ n); // array_chunk cut array (php4) // call_user_func_array call the specified function and pass the parameter array (php4) foreach ($ d as $ I => $ t) {if ($ I = 0) {$ r = $ t ;} else {array_multisort (array_map (function ($ v) {return $ v [0] ;}, $ r), $ r); $ r = array_map ('Array _ merge ', $ r, $ t );}}

In addition to simplifying array () to [] and using closures, there is nothing new
The closure can be rewritten using the create_function.

In addition to simplifying array () to [] and using closures, there is nothing new
The closure can be rewritten using the create_function.
Well, 5.3 does not have this syntax. How does one feel that php syntax and javascript are getting more and more like
Thanks to the moderator
I wrote one myself, comparison? Wordy

Function arr_combina3 () {$ args = func_get_args (); $ num = count ($ args); if (empty ($ args) {return array ();} else if ($ num = 1) {return $ args [0] ;}$ arr = array (); $ I = 0; $ j = 0; // currently, the array subscript $ last_index = $ num-1 is used. // The passed parameter array while ($ j <= $ last_index) is used cyclically in the input order) {if ($ j = $ last_index) {// when the array is currently used as the final parameter array // add each input parameter array to the combination $ tmp = $ arr [$ I]; foreach ($ args [$ j] as $ k => $ v) {$ t = $ tmp; $ t [] = $ v; $ arr [$ I] = $ t; ++ $ I;} // reverse loop lookup if the parameter array is used up for ($ n = $ J-1; $ n> = 0; -- $ n) {$ t = current ($ args [$ n]); // checks whether it has reached the end of the array if (empty ($ t )) {// if (0 = $ n) {// It is the end of the first array. The combination is complete and $ num = 0 is stopped; break 2;} reset ($ args [$ n]);} else {// when the end is not reached, modify the current array subscript $ j = $ n; // remove the final item $ arr [$ I] = array_slice ($ tmp, 0, $ n, true); break ;}} unset ($ tmp );} else {// add the value Currently pointed to by the current pointer in the array to the Combined Array $ arr [$ I] [] = current ($ args [$ j]); next ($ args [$ j]); // currently, the array pointer is used to move forward one bit + + $ j; // currently, the array subscript + 1} unset ($ args ); return $ arr ;}

function my_arr_combine(){$args = func_get_args();$final = array_shift($args);while($f = array_shift($args)){$r = array();foreach($final as $v){foreach($f as $v2){$r[] = array_merge(is_array($v)?$v:array($v),array($v2));}}$final = $r;}return $final;}$arr1 = array(1, 2, 3, 4);$arr2 = array(10, 20, 30, 40);$arr3 = array(100, 200, 300, 400);$data = array($arr1,$arr2,$arr3);echo "
";print_r(call_user_func_array('my_arr_combine', $data));

It's no longer ugly. add it to favorites.

function my_arr_combine(){$args = func_get_args();$final = array_shift($args);while($f = array_shift($args)){$r = array();foreach($final as $v){foreach($f as $v2){$r[] = array_merge(is_array($v)?$v:array($v),array($v2));}}$final = $r;}return $final;}$arr1 = array(1, 2, 3, 4);$arr2 = array(10, 20, 30, 40);$arr3 = array(100, 200, 300, 400);$data = array($arr1,$arr2,$arr3);echo "
";print_r(call_user_func_array('my_arr_combine', $data));

This is simple and clear

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.