PHP笛卡尔积实现算法示例

前端之家收集整理的这篇文章主要介绍了PHP笛卡尔积实现算法示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP笛卡尔积实现算法。分享给大家供大家参考,具体如下:

PHP;"> 1) { foreach ($str as $k => $val) { foreach ($first as $key => $value) { //最终实现的格式 1,76 //可根据具体需求进行变更 $str2[] = $val.','.$value; } } }else{ foreach ($first as $key => $value) { //最终实现的格式 1,76 //可根据具体需求进行变更 $str2[] = $value; } } //递归进行拼接 if(count($arr) > 0){ $str2 = cartesian($arr,$str2); } //返回最终笛卡尔积 return $str2; } $cartesian_product = cartesian($arr); print_r($cartesian_product); ?>

最终输出格式

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

小编这里参考前面一篇《》,给出一个php计算笛卡尔积的超简单算法示例如下:

运行结果:

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 )

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/15801.html

猜你在找的PHP相关文章