2017-08-16 6 views
1

コメントに基づいて説明を加えて編集しました。連想配列を使用したPHPループ

私はこのようになり、キーと値を持つダイナミック連想配列を有する:

array: ["apples" => 4 "bananas" => 4 "cherries" => 4 "dates" => 3] 

Iは、(動的nの)別のNサイズの配列を作成したいという意志直列に配列をループ。

例:

function makeArray($commonWords){ 
    $n = 6; 
    $result = array_fill(0,$n, ''); 
    $i = 0; 

    while (list($key, $value) = each($commonWords)) { 
    $result[$i] = $result[$i] . $key; 
    $i++; 
    } 

    return $result; 
} 

この出力を提供する:

(if n = 6): 

apples, cherries, apples 
bananas, dates, bananas 
cherries, apples, cherries 
dates, bananas 
apples, cherries 
bananas, dates 

Nレンジが

すべての値の1〜和Iが持っているコードは、これまでです
array:6 [▼ 
    0 => "apples" 
    1 => "bananas" 
    2 => "cherries" 
    3 => "dates" 
    4 => "" 
    5 => "" 
] 

しかし、5行目は "リンゴ"でなければならず、6番目のものは "バナナ"である必要があります。 その後、上記の例のように、 "apples"の後に "cherries"などが必要です。

これは説明を明確にします。

+1

たぶんそれは私ですが、私はそれを得ません – Andreas

答えて

0

私はあなたの質問を正しく理解していれば、私は解決策があると思います。

$nを関数内で定義するのではなく、引数リストに追加しました。

function makeArray($commonWords,$n=6){ 
    $result = array_fill(0,$n,''); 
    $c = 0; 
    // while we still have words to print out 
    while(array_sum($commonWords)){ 
     foreach($commonWords as $word=>$number){ 
      // if this word is still available 
      if($number > 0){ 
       // put the separator if we have looped through at least once 
       if($c >= $n){ 
        $result[($c % $n)] .= ", "; 
       } 
       $result[($c % $n)] .= $word; 
       // reduce the number of this word available 
       $commonWords[$word]--; 
       $c++; 
      } 
     } 
    } 
    return $result; 
} 
+0

ありがとうございました。あなたは私が必要としているものを正確に理解しており、これは優れた解決策です! – SuperOcean

0

あなたが達成しようとしていることは明らかではありませんが、次のことができます。あなたは

ここ

を説明しているが、第5行は、「りんご」にする必要がどのような

、第六は 「バナナ」にする必要があります。

は、サーキュラーリンクリストです。しかし、反復の回数は与えられた配列の合計値を超えることはできないので、これはある程度の循環リンクリストです。 wikiにリンクしています。

幸いなことにPHPはSplDoublyLinkedListクラスのStandard PHP Libraryにあります。しかし、我々は我々のニーズに応えるために、それを少しシェイプする必要があります。

class CircularLinkedListWithLimit extends SplDoublyLinkedList 
{ 
    protected $limit; 

    public function __construct($limit) 
    { 
     $this->limit = $limit; 
    } 

    public function next() 
    { 
     $this->limit -= 1; 
     parent::next(); 
    } 

    public function valid() 
    { 
     return $this->limit > 0 
      ? parent::valid() || $this->rewind() || true 
      : false; 
    } 
} 

は、このクラスを持つ私たちは私たちのリストを作成することができます。

$array = ["apples" => 4, "bananas" => 4, "cherries" => 4, "dates" => 3]; 

$limit = array_sum($array); 
$list = new CircularLinkedListWithLimit($limit); 

foreach ($array as $key => $_) { 
    $list->push($key); 
} 

この循環リストを持って、私たちは、私は、ハードコーディングされた(私たちのテーブルを読み込むことができ$nここでは簡単のために、しかし、あなたが機能でこれをラップすることができます):

$n = 6; 

$i = 0; 
$j = 0; 
$table = array_fill($i, $n, []); 

foreach ($list as $item) { 
    $table[$i][$j] = $item; 

    $i += 1; 

    if ($i >= $n) { 
     $i = 0; 
     $j += 1; 
    } 
} 

は、このテーブルを持って、あなたが好きなものを行うことができます。あなたはやや期待される出力の提供されるように、ここではそれを印刷するコードです:

echo implode(PHP_EOL, array_map(function ($row) { 
    return implode(', ', $row); 
}, $table)); 

これは

apples, cherries, apples 
bananas, dates, bananas 
cherries, apples, cherries 
dates, bananas 
apples, cherries 
bananas, dates 

になります。ここworking demoです。

ほとんどの場合、ビルトイン機能が使用されています。ネストされていない単純なループで、必要に応じてブートストラップします。これは、実際には高水準プログラミング言語のプログラミングの目標です。コードを書く回数が少ないので、システムに導入するバグが少なくなります。