【问题标题】:How to add more value to a single array in the same function?如何在同一函数中为单个数组添加更多值?
【发布时间】:2014-10-03 21:18:40
【问题描述】:

当我尝试以下代码时,我得到的只是第一个输出。我想将所有输出附加到数组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')) ;
?> 

【问题讨论】:

    标签: php arrays oop object web


    【解决方案1】:

    您从循环内的$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;
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 非常感谢。有用。你知道,这些都是我们新手缺乏的常见东西。非常感谢。
      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2021-10-25
      • 2018-06-07
      • 2020-07-18
      相关资源
      最近更新 更多