2014-10-03 70 views
0

當我嘗試下面的代碼時,我只得到第一個輸出。我想所有的輸出追加到數組self::$results使同一函數將返回數組,但在運行腳本後,該函數返回數組,但只有第一個輸出。這意味着,它只附加第一個輸出。如何在同一個函數中爲單個數組添加更多值?

<?php 

/** 
* @author Ewoenam 
* @copyright 2014 
* 
* @odj class book 
*/ 

class book 
{ 
    /** 
    * @array static $alpha; holds array of accepted words 
    */ 
    public static $alpha = array(); 
    /** 
    * @array static $result; holds array of correct answers 
    */ 
    public static $results; 
    /** 
    * @string protected $word; parametr 
    */ 
    protected $word; 
    /** 
    * @var static $alpha; holder of class instance getter 
    * returns self::getInstance 
    */ 
    public static $init; 

    /** 
    * @method static getInstance(); class instance getter 
    * returns self(); 
    */ 

    function __construct() 
    { 
     self::$alpha = array('love','life','health','power','money','God'); 
    } 

    public static function getInstance() 
    { 
     if (self::$init === null) 
     { 
      self::$init = new self(); 
      return self::$init; 
     } 
    } 

    /** 
    * @method static check() 
    * takes 1 param; self::$word 
    * returns bool 
    */ 
    public static function check($word) 
    { 

     for($i=0;$i<=count(self::$alpha)-1;$i++) 
     { 
      if(similar_text($word,self::$alpha[$i]) === strlen($word)) 
      { 
       return true; 
      } 

     } 
    } 





    /** 
    * @method static result() 
    * takes 1 param; self::check() 
    * returns bool 
    */ 
    public static function result($bool) 
    { 
     if($bool === true) 
     { 
      return 'correct'; 
     } 
     else 
     { 
      return 'wrong'; 
     } 
    } 

    /** 
    * @method static getter() 
    * takes 1 param; array of words to be searched 
    * returns array self::$results 
    */ 
    public static function getter($array) 
    { 
     self::$results = array(); 


     for($i = 0;$i<=count($array)-1;$i++) 
     { 


      // i want to add more thn one answers to to $result array but i get only the first answer. 
      //how do i ddo it? 
      self::$results[] = self::result(book::check($array[$i])); 

      return self::$results; 
     } 
    } 

} 




$array = array('love','ama','kofi','money','health','God'); 
print_r(book::getInstance()->getter($array)); 
var_dump(book::check('love')) ; 
?> 

回答

0

你從$getter返回循環中,所以添加的第一個元素後返回。你應該完成循環,然後返回:

public static function getter($array) 
{ 
    self::$results = array(); 
    for($i = 0;$i<=count($array)-1;$i++) 
    { 
     self::$results[] = self::result(book::check($array[$i])); 
    } 
    return self::$results; 
} 
+0

非常感謝。有用 – 2014-10-03 21:38:02

0

您在for循環中有您的return。這就是爲什麼當它經歷了第一次,它返回的功能,而不是繼續循環。

你的函數改成這樣,它會工作,你如何想它:

/** 
* @method static getter() 
* takes 1 param; array of words to be searched 
* returns array self::$results 
*/ 
public static function getter($array) 
{ 
    self::$results = array(); 


    for($i = 0;$i<=count($array)-1;$i++) 
    { 


     // i want to add more thn one answers to to $result array but i get only the first answer. 
     //how do i ddo it? 
     self::$results[] = self::result(book::check($array[$i])); 
    } 

    return self::$results; 
} 
+0

非常感謝。有用。你知道,這些是我們新手缺乏的常見事情。非常感謝。 – 2014-10-03 21:37:32

相關問題