变量未检测为数组

我正在尝试创建一个文档注册代码(尚未编写函数),并且我尝试进行回显以检查我的代码是否正常工作。不幸的是,它显然不起作用,因为我收到 $tag 不是数组的错误,而它实际上是数组。我得到的错误是:

警告:count():参数必须是在 C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php 第 37 行中实现 Countable 的数组或对象

注意:未初始化的字符串偏移:C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php 第 38 行标签中的 0

警告:count():参数必须是在 C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php 第 37 行实现 Countable 的数组或对象”

代码是:

class Paquete{

    //Variable

    public $tag = array();

    //Funcion

    public function guardar($tag){

        $this->tag = $tag;

    }

    public function grabar($tag){

        //INSERT....

        echo "Tag". $this->tag . "<br/>";

    }

    public function ciclos() {

        for($i = 0; $i < count($this->tag); $i++){

            Paquete::grabar($this->tag[$i]);

        }

    }

}


慕后森
浏览 63回答 1
1回答

扬帆大鱼

guardar()应该推$tag入 $this->tag数组,而不是用单个标签替换数组。另外,grabar不应该 echo $this-tag,它应该 echo$tag参数。并且应该将其声明为静态,因为它不需要使用$this.class Paquete{&nbsp; &nbsp; //Variable&nbsp; &nbsp; public $tag = array();&nbsp; &nbsp; //Funcion&nbsp; &nbsp; public function guardar($tag){&nbsp; &nbsp; &nbsp; &nbsp; $this->tag[] = $tag;&nbsp; &nbsp; }&nbsp; &nbsp; public static function grabar($tag){&nbsp; &nbsp; &nbsp; &nbsp; //INSERT....&nbsp; &nbsp; &nbsp; &nbsp; echo "Tag". $tag . "<br/>";&nbsp; &nbsp; }&nbsp; &nbsp; public function ciclos() {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($this->tag as $tag) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paquete::grabar($tag);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP