Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Cartesian Product of Array by php

Shulou Source: shulou.com Published: 2022-06-02 07:42:54 10月14日 Update

This article mainly introduces "php how to achieve the Cartesian product of the array". In the daily operation, I believe many people have doubts about how to realize the Cartesian product of the array in php. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to realize the Cartesian product of the array by php". Next, please follow the editor to study!

Php to achieve the Cartesian product of the array method: 1, create a PHP example file; 2, define an array; 3, through the "function dikaer ($arr) {.}" to achieve the Cartesian product of the array operation.

This article operating environment: Windows7 system, PHP7.4 version, Dell G3 computer.

How does php implement the Cartesian product of arrays?

An example of Cartesian Product Operation of an Array implemented by PHP

The Cartesian product of an array is quite useful in practice. For example, it is often used to calculate the specifications of goods. Here is an implementation, as follows

$arr = array (array (2), array (6pc7), array ('axiang maxim); function dikaer ($arr) {$arr1 = array (); $result = array_shift ($arr); while ($arr2 = array_shift ($arr)) {$arr1 = $result; $result = array (); foreach ($arr1 as $v) {foreach ($arr2 as $v2) {if (! is_array ($v)) $v = array ($v); if (! is_array ($v2)) $v2 = array ($v2) $result [] = array_merge_recursive ($vmaine v2);} return $result;}

The output of the above example is as follows:

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

If you need to output a result in the form of a string, you can change the code like this

Function dikaer ($arr) {$arr1 = array (); $result = array_shift ($arr); while ($arr2 = array_shift ($arr)) {$arr1 = $result; $result = array (); foreach ($arr1 as $v) {foreach ($arr2 as $v2) {$result [] = $return $result;}

The output is as follows:

Array ([0] = > 2pint 6rem a [1] = > 2pje 6rem b [2] = > 2je 6r c [3] = > 2LJ 7LI a [4] = > 2LI 7LI c). At this point, the study on "how php realizes the Cartesian product of arrays" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Array Descartes learning results output code methods more examples help operations utility next examples goods characters strings actual forms files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Shulou Information MySQL Xiaomi