使用php从多维数组中导出所有可能的组合(或排列)

问题描述 投票:0回答:1

我有这个多维数组,我希望计算数组元素的各种组合。这是数组:

$komponenten = array(
"Rrahmen" => array(
    "Laenge" => array("400", "500"),
    "Breite" => array("400", "500"),
    "Hoehe" => array("400", "500"),
    "Material" => array("Aluminium Eloxiert"),
    "Farbe" => array("Schwarz", "Silber"),
    "Gehaeuseverkleidung" => array("Ja", "Nein")
),

"Druckbett" => array(
    "Laenge" => array("400", "500"),
    "Breite" => array("400", "500"),
    "Hoehe" => array("400", "500")
),

"Druckkopf" => array(
    "Anzahl Druckmaterielien" => array("1", "2"),
    "Filament" => array("ABS", "PLA"),
    "Filamen Groesse" => array("1.7", "3.0")
),

"Schnittstelle" => array(
    "USB" => array("Ja", "Nein"),
    "LAN" => array("Ja", "Nein"),
    "WLAN" => array("Ja", "Nein"),
    "Bluetooth" => array("Ja", "Nein"),
    "SD Card" => array("Ja", "Nein")
),

"Accessories" => array(
    "Energieversorgung" => array("12V", "24V"),
    "Zustellung" => array("Aufgebaut", "Bausatz")
)

);

以下是两种预期的可能组合(每种组合具有相同数量的元素,只有最终属性会发生变化......)。任何帮助都非常令人沮丧!

[1] = array(
"Rrahmen" => array(
    "Laenge" = 400,
    "Breite" = 400,
    "Hoehe" = 400,
    "Material" = "Aluminium Eloxiert",
    "Farbe" ="Silber",
    "Gehaeuseverkleidung" = "Nein"
),

"Druckbett" => array(
    "Laenge" = "500",
    "Breite" = "400",
    "Hoehe" = "500"
),

"Druckkopf" => array(
    "Anzahl Druckmaterielien" = 1,
    "Filament" = "ABS",
    "Filamen Groesse" = 3.0
),

"Schnittstelle" => array(
    "USB" = "Ja",
    "LAN" = "Nein",
    "WLAN" = "Nein",
    "Bluetooth" = "Nein",
    "SD Card" = "Nein",
),

"Accessories" => array(
    "Energieversorgung" = "24V",
    "Zustellung" = "Aufgebaut"
)

[2] = array(
"Rrahmen" => array(
    "Laenge" = 400,
    "Breite" = 500,
    "Hoehe" = 400,
    "Material" = "Aluminium Eloxiert",
    "Farbe" ="Schwarz",
    "Gehaeuseverkleidung" = "Ja"
),

"Druckbett" => array(
    "Laenge" = "500",
    "Breite" = "500",
    "Hoehe" = "500"
),

"Druckkopf" => array(
    "Anzahl Druckmaterielien" = 1,
    "Filament" = "ABS",
    "Filamen Groesse" = 3.0
),

"Schnittstelle" => array(
    "USB" = "Ja",
    "LAN" = "Nein",
    "WLAN" = "Nein",
    "Bluetooth" = "Nein",
    "SD Card" = "Nein",
),

"Accessories" => array(
    "Energieversorgung" = "24V",
    "Zustellung" = "Aufgebaut"
)
php combinations permutation cartesian-product
1个回答
0
投票

我将数组拆分为单个数组

$rahmen = array(
    "Laenge" => array("400", "500"),
    "Breite" => array("400", "500"),
    "Hoehe" => array("400", "500"),
    "Material" => array("Aluminium Eloxiert"),
    "Farbe" => array("Schwarz", "Silber"),
    "Gehaeuseverkleidung" => array("Ja", "Nein")
);

$druckbett = array(
    "Laenge" => array("400", "500"),
    "Breite" => array("400", "500"),
    "Hoehe" => array("400", "500")
);

$druckkopf = array(
    "Anzahl Druckmaterielien" => array("1", "2"),
    "Filament" => array("ABS", "PLA"),
    "Filamen Groesse" => array("1.7", "3.0")
);

$schnittstelle = array(
    "USB" => array("Ja", "Nein"),
    "LAN" => array("Ja", "Nein"),
    "WLAN" => array("Ja", "Nein"),
    "Bluetooth" => array("Ja", "Nein"),
    "SD Card" => array("Ja", "Nein")
);

$accessories = array(
    "Energieversorgung" => array("12V", "24V"),
    "Zustellung" => array("Aufgebaut", "Bausatz")
);


$components = array(
    "Rahmen" => $rahmen, 
    "Druckbett" => $druckbett, 
    "Druckkopf" => $druckkopf, 
    "Schnittstelle" => $schnittstelle, 
    "Accessories" => $accessories);

$components = array("Rahmen", "Druckbett", "Druckkopf", "Schnittstelle", "Accessories");

并使用以下函数来获得解决方案:

function cartesian(array $input)
    {
        $result = [[]];
        foreach ($input as $key => $values) {
            $append = [];
            foreach ($values as $value) {
                foreach ($result as $data) {
                    $append[] = $data + [$key => $value];
                }
            }
            $result = $append;
        }

        return $result;
    }


$print1 = cartesian($rahmen);

$print2 = cartesian($druckbett);

$print3 = cartesian($druckkopf);

$print4 = cartesian($schnittstelle);

$print5 = cartesian($accessories);

$prints = array($print1, $print2, $print3, $print4, $print5);

$print = array_combine($components, $prints);

echo "<pre>";
print_r($print);

//print_r(cartesian($print));
echo "</pre>";
© www.soinside.com 2019 - 2024. All rights reserved.