2013-04-08 87 views
2

我試圖在PHP中練習/學習foreach循環。我瞭解基本的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個。所以它需要有點動態。

1解決方案嘗試

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; 
} 

然而,這將把所有我的話在單個陣列中。這不符合我所尋找的要求。

+0

如果您詳細闡述了您的輸入和預期輸出,可能會更容易建立模式。 – Emissary 2013-04-08 21:46:45

+0

我已更新。這是否有助於讓我更清楚我想完成什麼? – EnigmaRM 2013-04-08 21:49:37

+0

不是真正的交配 - 嘗試用你的「SEO」單詞顯示一個實際的測試用例,用$ data中的3個數組,然後輸出所需的輸出 - 否則它們不清楚它們之間的關係是什麼。 – Emissary 2013-04-08 21:58:22

回答

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, ''); 
    } 
} 

我改變了一些變數名稱等,但這種奇妙的作品!

+0

雖然完全可以*回答並接受*給你自己的問題 - 這部分代碼塊實際上並不對其他任何人有用,因爲它顯然是更大類的一部分。您應該優化您的答案,使其與原始問題的簡化性質相匹配,以便直接複製/粘貼將展示一個可行的示例。也請考慮接受關閉該主題的答案。 – Emissary 2013-04-11 08:24:57

+0

我明白你在說什麼。在這種情況下,類的部分只是一個包裝。其餘代碼將在我的控制器中結束。所以這些功能本身可以被複制/粘貼。這不夠嗎?我顯然比堆棧溢出更新一些,所以我不知道一些被接受的做法。所以讓我知道。 – EnigmaRM 2013-04-11 22:01:40

2

你需要做的是建立一個功能,將做到這對你來說,遞歸,像....

// function for building recursive tree arrays 
function recursive($array) { 
    foreach($array as $key => $value) { 
     if(is_array($value)) { 
      // if it is an array, it will run again 
      recursive($value); 
     } else { 
      // do something with values here 
     } 
    } 
    // return your result set here 
} 

$data = array(
     array('key', 'key2'), 
     array('word', 'word2','word3'), 
     array(array('more','levels','than','you','need'), 'then','some','more') 
    ); 

$result = recursive($data); 

它的粗糙,但是這取決於你在做什麼,你只需要添加您的流程以獲得您的總體最終結果。

基本上,無論數組的深度有多少,或者數組結構的順序如何,它都會繼續運行。所有你需要做的就是確保你發送(返回)函數每次掃描的結果,這取決於你對每個級別做了什麼。

儘管如此,它應該能夠讓你很好。

+0

我遵循了類似於你所擁有的東西。但是這會將所有內容都返回到單個數組中,而不是多個數組。我相信這將是一個簡單的調整。基於我添加到第一個問題的代碼,你建議什麼? – EnigmaRM 2013-04-08 22:29:02

1

好的,這裏是一個非遞歸解決方案,我相信只要您的$data陣列保持二維,我們就會遵循所需的模式。我不會承認自己多少次嘗試這實際上就把我......

$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; 
    }); 

run code* PHP 5.4 +


稍微偏離主題,但值得一提的 - 這不是我不知道你將這些「關鍵詞」放入什麼樣的背景中,但是像這樣發送垃圾郵件可能會構成糟糕的SEO。

+0

謝謝。但是,陣列肯定不會保持二維。我這樣說是爲了更簡單地解釋發生了什麼。哈。但我會玩弄你有什麼,看看我能否擴展一下。 – EnigmaRM 2013-04-09 17:09:37

+0

你對SEO的垃圾郵件是正確的。這是爲了幫助生成可能的關鍵字列表。然後,我會出去使用AdWords工具,看看哪些是富有成效的。通過這種方式,我可以輕鬆查看區域並識別與該區域相關的關鍵詞,並輕鬆創建大量有針對性的關鍵字。 (不知道這是否合理)。 – EnigmaRM 2013-04-09 17:11:20