简体   繁体   中英

PHP:: Get all available options inside array

My brain is on fire :'(

I want to get all available options from $vowels but without repeat the same letter in the same line.

<?php

$vowels=array("A","E","I","O","U");

$row=3;
$column=3;

echo '<pre>';

do{

        foreach ($vowels as &$value)
        {

                 //Magic Goes Here :P
        }

        $row--;

}while($row<>0);

echo '</pre>';

so (for example) because of there is 5 options here ("A","E","I","O","U") but there the row is limited to 3 the maximum option/S in a single line is 3 but just to make it simple lets just assume :

$row=2;
$column=2;

so the results:

AE

EA

AI

IA

AO

OA

AU

UA


EA

AE

EI

IE

EO

OE

EU

UA


IA

AI

IE

IO

IU

UI

etc... Thanks for helping in advance! :)

<?php

$vowels=array("A","E","I","O","U");

echo '<pre>';

    foreach ($vowels as $value)
    {
        foreach ($vowels as $value1){
            if($value1 != $value)
                echo $value.$value1.'<br>';     
        }
    }

echo '</pre>';

I would suggest to use recursion:

function recurrency($vowels, $cols, $level, $used) {
    $i = -1; // EDIT to make loop index increment in one place
    foreach ($vowels as $value) {
        $i++;
        if (in_array($i, $used)) {
            continue;
        }
        $newUsed = $used;
        $newUsed[] = $i;
        if ($level < $cols && count($vowels) > $level) {
            recurrency($vowels, $cols, $level + 1, $newUsed);
        } else {
            foreach ($newUsed as $index) {
                echo $vowels[$index];
            }
            echo "<br />\n";
        }
        if ($level == 1) {
            echo "<hr />\n\n";
        }
    }
}

$vowels=array("A","E","I","O","U");

$row=3;
$column=3;

echo '<pre>';

recurrency($vowels, $column, 1, []);

echo '</pre>';

It's not clear solution and I didn't get how to use variable $row , but I guess this is the idea you are asking about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM