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;
}
ただし、これはすべての単語を単一の配列で返します。それは私が探しているものの要件を満たしていません。