我如何选择一个数组中的每一项以确保每组都输出

时间:2018-09-24 14:40:17

标签: php laravel

让我们说我有一个数字数组:(?<=\$page\s=\s').+?(?=';\sinclude\('assets/services\.php)

如何遍历此Dim strColLetter As String Dim strActiveColLetter As String Dim strColLetter As String Dim strActiveColLetter As String Dim strBulkNum As String Dim intLastCol As Long Dim i As Long Dim j As Long Dim intLastRow As Long Dim rngProntoItemDataBulkNum As Range Dim rngProntoItemDataRefNum As Range Dim wksh As Worksheet Dim wbk As Workbook Set wbk = Workbooks("Master - Pronto_Item_Data.xlsx") Set wksh = Workbooks("Master - Pronto_Item_Data.xlsx").Sheets("Pronto_Data") intLastRow = ThisWorkbook.Sheets("WorkFile4").Cells(Rows.Count, 1).End(xlUp).Row intLastCol = ThisWorkbook.Sheets("WorkFile4").Cells(1, Columns.Count).End(xlToLeft).Column strColLetter = Split(Cells(1, intLastCol + 2).Address, "$")(1) Set rngProntoItemDataBulkNum = wksh.Range("ProntoItemDataBulkNum") Set rngProntoItemDataRefNum = wksh.Range("ProntoItemDataRefNum") For j = 3 To intLastRow - 1 'looping through the rows For i = 1 To 5 'looping across five columns strActiveColLetter = Split(Cells(1, intLastCol + 1 + i).Address, "$")(1) ThisWorkbook.Sheets("WorkFile4").Cells(j, intLastCol + 1 + i).FormulaArray = "=IFERROR(INDEX('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!" & rngProntoItemDataBulkNum.Address(False, True) & ", SMALL(IF('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!" & rngProntoItemDataRefNum.Address(False, True) & "=$A" & j & ",ROW('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!" & rngProntoItemDataRefNum.Address(False, True) & ")),COLUMNS($" & strColLetter & "$" & j & ":" & strActiveColLetter & j & "))),"""")" strBulkNum = Evaluate("=IFERROR(INDEX('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!$A:$A, SMALL(IF('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!$N:$N=$A" & j & ",ROW('[Master - Pronto_Item_Data.xlsx]Pronto_Data'!$N:$N)),COLUMNS($" & strColLetter & "$" & j & ":" & strActiveColLetter & j & "))),"""")") Next i Next j 以创建可能的permutations的数组。

我期望输出如下: [1,2,3]array[1,2][1,3][2,1][2,3]

3 个答案:

答案 0 :(得分:1)

  • 同一输入数组所需的简单嵌套循环

执行以下操作:

$input = array(1,2,3);

$output = array();

// to get all possible permutations
// for first value in the permutation, loop over all array values
foreach ($input as $value1) {

    // for second value in the permutation, loop again similarly
    foreach ($input as $value2) {

        if ($value1 !== $value2)  // dont consider same values
            $output[] = array($value1, $value2);
    }
}

答案 1 :(得分:0)

如果我正确理解了这个问题,您想拥有一个包含X个整数的数组,并从该数组中获取两个不同整数的所有可能组合吗?

这可以通过两个for循环来完成,一个循环遍历数组的每个元素,另一个将该元素与其他每个元素组合在一起。

for(int $x = 0; $x < count($arr1); $x++) {
    for(int $y = 0; $y < count($arr1); $y++) {
        if ($x == $y || $arr1[$x] == $arr1[$y]) {
            continue;
        } else {
            array_push($output, [$arr1[$x], $arr1[$y]]);
        }
    }
}

答案 2 :(得分:0)

看您的示例,我假设您不想重复数字。因此,我使用一个内部循环执行了一个循环,并在添加到打印数组之前比较了数字!我的第一个stackoverflow提交,所以我希望得到一些反馈!

$arrayToParse = [1, 2, 3];
$arrayToPrint = [];

foreach($arrayToParse as $num1){
    foreach($arrayToParse as $num2){
        if($num1 != $num2){
            array_push($arrayToPrint, [$num1, $num2]);
        }
    }
}

print_r($arrayToPrint);

// OUTPUT
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
        )

    [2] => Array
        (
            [0] => 2
            [1] => 1
        )

    [3] => Array
        (
            [0] => 2
            [1] => 3
        )

    [4] => Array
        (
            [0] => 3
            [1] => 1
        )

    [5] => Array
        (
            [0] => 3
            [1] => 2
        )

)