2013-11-22 44 views
0

我一直在編碼和改善jszobody提供我可以在這個LINK它讓我成爲我REFFERENCE發現的代碼,但它導致了我的錯誤Fatal error: Using $this when not in object context

PHP:

<?php 
     function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array()) 
     { 

      // initialize results array 
      if ($firstTime) 
      { 
       $this->permutationsResultsArray = array(); 
      } 

      // find first sub array and iterate through it 
       $thisArray = array_shift($mdArray); 

      foreach ($thisArray as $key => $elem) 
      { 
       // if this number has already been used skip this possible permutation 
       if (in_array($elem, $tempArray)) 
       { 
        continue; 
       } 
        $tempArray[] = $elem; 

       if (count($mdArray) == 0) 
       { 
        $this->permutationsResultsArray[] = $tempArray; 
       } 
       else 
       { 
        $this->getAllPossiblePermutations($mdArray, false, $tempArray); 
       } 
        array_pop($tempArray); 
      } 
     } 

    $traits = array 
    (
     array('Happy', 'Sad', 'Angry', 'Hopeful'), 
     array('Outgoing', 'Introverted'), 
     array('Tall', 'Short', 'Medium'), 
     array('Handsome', 'Plain', 'Ugly') 
    ); 

    print_r(getAllPossiblePermutations($traits)); 
?> 

預期輸出:

Array ([0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] =>)

沒我有什麼錯?

+2

提供不符合您所粘貼代碼中的鏈接。 –

+2

當不在對象上下文中時,您正在使用'$ this'。 –

+0

錯誤指出,您正在課外使用$。 – James

回答

2

你有很多referemces類變量,像這樣:

$this->permutationsResultsArray = array(); 

而PHP的抱怨,因爲這個功能不是一個類的方法。它會工作,如果你只是刪除this->等你拿:

$permutationsResultsArray = array(); 

此外,當你完成所有的工作,你從來沒有真正返回的結果。像這樣:

return $permutationsResultsArray; 

有一個問題,它雖然。您正在重新使用,並且您不會在第一輪中創建該數組,但是您的代碼會像使用它定義一樣來使用它。

BTW:你的函數可以用3個foreach循環更容易:

function getCombinations($traits) 
{ 
    $combinations = array(''); 
    foreach($traits as $trait_level) { 
     $new_combinations = array(); 
     foreach ($combinations as $comb) { 
      foreach ($trait_level as $trait){ 
       $new_combinations[] = "$comb $trait"; 
      } 
     } 
     $combinations = $new_combinations; 
    } 
    return $combinations; 
} 
+0

不給我輸出:( – Darline

+0

你能給我舉一個例子嗎? – Darline

+0

@Darline什麼的例子? – Sylwester

0

函數沒有自引用$this。其實,除去所有$this->引用,使$ tempArray一個通過引用傳遞,而不是按價值,你的代碼應該工作...

function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array()) 
+0

你能告訴我一個例子 – Darline

+0

看看我放在那裏的代碼,&$ tempArray在它前面有一個&符號。這使得它成爲「參考」電話。現在在您的函數中刪除對$ this的所有引用。 – TrippyD

相關問題