2016-06-12 78 views
0

我有三個陣列像這樣:如何不對稱地打印一些陣列?

$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 

正如你看到的沒有任何紀律。這只是一個不合常理的序列。實際上,這些數組要大得多,我無法手動處理它們。我只想知道我該如何管理這樣的事情?在這種情況下我應該怎麼做?

我通常通過嵌套循環來做這些事情。但現在沒有任何順序..!我現在該怎麼做?

+1

如果沒有訂單!所以沒有算法來做這樣的事情。你如何設計一個你沒有任何規則的解決方案。我不知道也許我錯了。 –

+0

@ SiamakA.Motlagh是的沒有秩序..但其結構總是不變的..!我的意思是總是第一項「$ arr1」應該打印9次,其第二項打印4次。等其他陣列... –

+1

爲什麼你需要這樣做呢?我敢肯定,你走錯了方向去得到你想要的東西。告訴我們根本問題,那麼也許我們可以提供幫助。 – Dray

回答

1

您的評論,還有一些關於這些陣列的「結構」的附加信息:

是的沒有秩序..但其結構總是不變的..!我的意思是總是第一項$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數組。

+0

幹得好。 。 。 +1 –

+0

@Stack我喜歡你的符號+1:D –

+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

+0

什麼都不是隨機的。 –

+0

好的。 。 。 thx +1 –