PHP 2D 私有数组的未定义索引



我已经坚持了一段时间,不确定问题所在。

也许这里有人可能会有一些很好的见解?

这是"代码":

class File extends Stackable{
    private $data;
    function set_data($array){
        foreach($array as $row => $data)
            foreach($data as $col => $val)
                $this->data[$row][$col]=$val;
                echo $this->data[$row][$col];                    
    } 
}

其中它指出在echo上有一个Undefined index : $col,其中$col通常是一个字母。

可以假定已设置$row

也许我没有提供足够的细节,并且可能存在其他依赖项,如果是这样,请告诉我。

需要注意的一件事是在这里使用 php pthreads,尽管我不认为这是原因,因为错误仍然发生在 1 个线程上。

提前感谢您的任何帮助。

在你的

第二个foreach中您必须将代码放在 {} 之间,例如:

     foreach($data as $col => $val){
                    $this->data[$row][$col]=$val;
                    echo $this->data[$row][$col]; 
}

echo $this->data[$row][$col]; 是 out for each , $col 和 $val 没有定义。

成员 "data" 只是一个普通的数组,由于 pthreads 对象的工作方式,您正在丢失维度; 您不想使用成员"data",因此没有必要:

<?php
class File extends Stackable {
    /* in pthreads you are responsible for the objects you create */
    /* so we accept an array by reference to store dimensions */
    public function set_data($array, &$files){
         foreach($array as $row => $data) {
            foreach($data as $col => $val) {
                 /* force this vector into existence */
                 if (!isset($this[$row])) {
                    $this[$row] = $files[] = new File();
                 }
                 $this[$row][$col]=$val;
            }
         }                  
    } 
    public function run() {}
}
$files = array();
$f = new File();
$f->set_data(array("test" => array($_SERVER)), $files);
var_dump($f);
?>

您应该记住,pthreads 对象具有安全开销,因此尽可能少地循环它们的成员,在理想世界中,来到 setData 的$array已经是合适的类型......

最新更新