2

PHPでforeachループを練習/学習しようとしています。基本的な foreach を理解しています。しかし、私は多次元に苦労しています。

私はそれをテストするための配列を持っています:

$data = array(
        array('New York', 'New York City'),
        array('hotel', 'lodging','motel'),
        array('cheap')
    );

私がやりたいことは、可能なすべての組み合わせをループして、それを独自の配列に割り当てることです(後で列として表示されます)。

First column ('New York', 'New York City')
second column ('New York hotel', 'New York lodging', 'New York motel')
third column ('New York City hotel', 'New York City lodging', 'New York City motel')
fourth column ('New York hotel cheap', 'New York lodging cheap', 'New York motel cheap')
fifth column ('New York City hotel cheap', 'New York City lodging cheap', 'New York City motel cheap')

どうすればこれを達成できますか? 私はいくつかのことを試しましたが、近づくことさえできませんでした。

更新 私の最終目標は、個人がアイテムのリストを取得し、可能なすべてのキーワードを見つけようとすることです。これは、使用を検討できるさまざまな SEO キーワードを特定するのに役立ちます。そのため、Data 配列に 2 つのサブ配列がある場合もあれば、3 つまたは 4 つの場合もあります。したがって、少し動的にする必要があります。

最初の解決策の試み

public function recurseful($start, $args)
{
    if (is_array($args))
        {
            foreach ($args[0] as $value)
            {
                $this->output[] = trim("{$start} {$value}");
                if (count($args) > 1)
                {
                    $this->recurseful($value, array_slice($args, 1));
                }

            }
        }
    return;
}

ただし、これはすべての単語を単一の配列で返します。それは私が探しているものの要件を満たしていません。

4

3 に答える 3

1

$dataさて、これは、配列が2次元のままである限り、目的のパターンに従うと信じている非再帰的なソリューションです。私はこれが実際に私を何回試みたかを認めるつもりはありません...

$cols = array(array_shift($data));
while($row = array_shift($data))
  array_walk(count($cols)<=1 ? $cols : array_slice($cols, 1), 
  function($col) use($row, &$cols){
    foreach($col as $i => $prev)
      foreach($row as $j => $word)
          $tmp[count($cols)===1?$i:$j][] = "$prev $word";
    foreach($tmp as $t) $cols[] = $t;
  });

コードを実行 * PHP 5.4 +


トピックから少し外れますが、言及する価値があります。これらの「キーワード」をどのようなコンテキストに入れているのかはわかりませんが、このようにスパムを送信すると、悪い SEO になる可能性があります。

于 2013-04-09T10:04:27.837 に答える
0

大丈夫。さらに数時間、職場で多くのコラボレーションを行った後、次のようになります。

function permutate($data, $limit){
    $this->limit = $limit;
    $this->data = $data;
    $this->numLevels = count($this->data);

    $this->possiblePermutations = 1;
    foreach ($this->data as $array){
        $this->possiblePermutations *= count($array);
    }
    for ($i = 0; $i < $this->numLevels - 0; $i++){
        $this->permutations[$i] = array();
    }

    $this->recurse(0, 0, '');

    return $this->permutations;
}

private function recurse($currentLevel, $level, $string){
    if ($this->numPerms == $this->limit) 
        return;

    foreach ($this->data[$level] as $val){
        if ($this->numPerms == $this->limit) 
            return;

        $newString = "$string $val";
        if ($level == $currentLevel){
            $this->permutations[$level][] = trim($newString);
            $this->numPerms++;
        }

        if ($level < $this->numLevels - 1 AND $level <= $currentLevel){
            $this->recurse($currentLevel, $level + 1, $newString);
        }
    }

    if (! $level AND $currentLevel < $this->numLevels){
        $this->recurse($currentLevel + 1, 0, '');
    }
}

いくつかの変数名などを変更しましたが、これは素晴らしく機能します!

于 2013-04-09T22:49:27.440 に答える