PHP Cartesian Product Implementation Algorithm Example

  • 2021-10-27 06:39:14
  • OfStack

In this paper, an example is given to describe the implementation algorithm of PHP Cartesian product. Share it for your reference, as follows:


<?php
$arr = array(array(1,3,4,5),array(3,5,7,9),array(76,6,1,0));
/**
**  Realization 2 Cartesian product combination of dimensional arrays 
** $arr  To carry out Cartesian product 2 Dimensional array 
** $str  Cartesian product combination finally realized , Don't write 
** @return array
**/
function cartesian($arr,$str = array()){
  // Remove the number 1 Elements 
  $first = array_shift($arr);
  // Judge whether it is the first 1 Secondary splicing 
  if(count($str) > 1) {
    foreach ($str as $k => $val) {
      foreach ($first as $key => $value) {
        // The format of the final implementation  1,3,76
        // It can be changed according to specific requirements 
        $str2[] = $val.','.$value;
      }
    }
  }else{
    foreach ($first as $key => $value) {
      // The format of the final implementation  1,3,76
      // It can be changed according to specific requirements 
      $str2[] = $value;
    }
  }
  // Recursive splicing 
  if(count($arr) > 0){
    $str2 = cartesian($arr,$str2);
  }
  // Returns the final Cartesian product 
  return $str2;
}
$cartesian_product = cartesian($arr);
print_r($cartesian_product);
?>

Final output format

Array
(
[0] = > 1,3,76
[1] = > 1,3,6
[2] = > 1,3,1
[3] = > 1,3,0
[4] = > 1,5,76
[5] = > 1,5,6
[6] = > 1,5,1
[7] = > 1,5,0
[8] = > 1,7,76
[9] = > 1,7,6
[10] = > 1,7,1
[11] = > 1,7,0
[12] = > 1,9,76
[13] = > 1,9,6
[14] = > 1,9,1
[15] = > 1,9,0
[16] = > 3,3,76
[17] = > 3,3,6
[18] = > 3,3,1
[19] = > 3,3,0
[20] = > 3,5,76
[21] = > 3,5,6
[22] = > 3,5,1
[23] = > 3,5,0
[24] = > 3,7,76
[25] = > 3,7,6
[26] = > 3,7,1
[27] = > 3,7,0
[28] = > 3,9,76
[29] = > 3,9,6
[30] = > 3,9,1
[31] = > 3,9,0
[32] = > 4,3,76
[33] = > 4,3,6
[34] = > 4,3,1
[35] = > 4,3,0
[36] = > 4,5,76
[37] = > 4,5,6
[38] = > 4,5,1
[39] = > 4,5,0
[40] = > 4,7,76
[41] = > 4,7,6
[42] = > 4,7,1
[43] = > 4,7,0
[44] = > 4,9,76
[45] = > 4,9,6
[46] = > 4,9,1
[47] = > 4,9,0
[48] = > 5,3,76
[49] = > 5,3,6
[50] = > 5,3,1
[51] = > 5,3,0
[52] = > 5,5,76
[53] = > 5,5,6
[54] = > 5,5,1
[55] = > 5,5,0
[56] = > 5,7,76
[57] = > 5,7,6
[58] = > 5,7,1
[59] = > 5,7,0
[60] = > 5,9,76
[61] = > 5,9,6
[62] = > 5,9,1
[63] = > 5,9,0
)

This site refers to the previous article "JavaScript Cartesian product ultra-simple implementation algorithm", and gives an example of php ultra-simple algorithm for calculating Cartesian product as follows:


<?php
function cartesian($arr1,$arr2){
  $relarr = array();
  foreach($arr1 as $v1){
    foreach($arr2 as $v2){
      array_push($relarr,$v1.",".$v2);
    }
  }
  return $relarr;
}
// Usage example 
$a = array('1','2','3');
$b = array('a','b',c);
print_r(cartesian($a,$b));
?>

Run results:

Array
(
[0] = > 1,a
[1] = > 1,b
[2] = > 1,c
[3] = > 2,a
[4] = > 2,b
[5] = > 2,c
[6] = > 3,a
[7] = > 3,b
[8] = > 3,c
)

For more readers interested in PHP related contents, please check the special topics of this site: Summary of PHP Mathematical Operation Skills, Encyclopedia of PHP Array (Array) Operation Skills, Summary of PHP Operation and Operator Usage, Summary of php String (string) Usage, Summary of PHP Common Traversal Algorithms and Skills, Tutorial of PHP Data Structure and Algorithm and Summary of php Programming Algorithms

I hope this article is helpful to everyone's PHP programming.


Related articles: