如何不对称地打印一些数组?

时间:2016-06-12 12:29:00

标签: php arrays

我有三个这样的数组:

$arr1 = array ("one", "two");
$arr2 = array ("red", "blue", "white", "green", "pink");
$arr3 = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");

我正在尝试打印这样的东西:

one - red - 1
one - red - 2
one - red - 3
one - blue - 4
one - blue - 5
one - white - 6
one - white - 7
one - white - 8
one - white - 9
two - green - 10
two - pink - 11
two - pink 12
two - pink 13

如你所见,没有任何纪律。这只是一个不变的常数序列。实际上,这些阵列要大得多,我无法手动处理它们。我只想知道如何处理这样的事情?在这种情况下我该怎么做?

我通常通过嵌套循环来做这些事情。但现在没有任何订单..!我现在该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取随机行:

<?php
$arr1 = array ("one", "two");
$arr2 = array ("red", "blue", "white", "green", "pink");
$arr3 = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");

echo $arr1[rand (0, (count($arr1) - 1))].' - '.$arr2[rand (0, (count($arr2) - 1))].' - '.$arr3[rand (0, (count($arr3) - 1))];

要产生预期的输出,您可以使用以下内容:

<?php
$arr1 = array ("one", "two");
$arr2 = array ("red", "blue", "white", "green", "pink");
$arr3 = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");

for ($i = 0; $i < count($arr3); $i++) {
    echo $arr1[rand (0, (count($arr1) - 1))].' - '.$arr2[rand (0, (count($arr2) - 1))].' - '.$arr3[$i]."\n";
}

演示:https://3v4l.org/PDL5N

答案 1 :(得分:1)

根据您的评论,还有一些关于&#39;结构的其他信息。这些数组:

  

是没有订单..但它的结构总是不变的..!我的意思是始终$arr1的第一项应该打印9次,第二项应该打印4次..!等等其他数组...

你没有提供,但让它看起来像你答案中的输出。

$arr1 = array("one", "two");
$arr2 = array("red", "blue", "white", "green", "pink");
$arr3 = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");

$structure = array(
     array(9, 4), // sequence lengths for $arr1
                  // = repeat first item 9 times, second 4 times
     array(3, 2, 4, 1, 3), // for $arr2
     array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), // for $arr3
);

function createOutput($arrays, $structure, $length){
    $ret = array();
    $seq_pos = array();
    $seq_iter = array();
    for ($i = 0; $i < count($structure); $i++){
        $seq_pos[$i] = 0;
        $seq_iter[$i] = 0;
    }
    for ($i = 0; $i < $length; $i++){
        $ret[$i] = array();
        for ($j = 0; $j < count($structure); $j++){
            $ret[$i][$j] = $arrays[$j][$seq_pos[$j]];
            $seq_iter[$j]++;
            if ($seq_iter[$j] >= $structure[$j][$seq_pos[$j]]){
                $seq_iter[$j] = 0;
                $seq_pos[$j]++;
                if ($seq_pos[$j] >= count($structure[$j])){
                    $seq_pos[$j] = 0;
                }
            }
        }
    }
    return $ret;
}

$out = createOutput(
    array($arr1, $arr2, $arr3),
    $structure,
    13
);
for ($i = 0; $i < count($out); $i++){
    echo implode($out[$i], " - ")."\n";
}

产生:

one - red - 1
one - red - 2
one - red - 3
one - blue - 4
one - blue - 5
one - white - 6
one - white - 7
one - white - 8
one - white - 9
two - green - 10
two - pink - 11
two - pink - 12
two - pink - 13

该函数的工作方式是它为每个数组保留各种迭代器。如果项目重复了足够的次数,它将步入数组中的下一个位置。再一次,你没有在答案中提供足够的信息,但你坚持认为结构是不变的。如果是这样,您需要根据所需序列的长度微调(或生成)$structure数组。