使用方法填充时未填充公共数组属性


public array property not populated when using method to populate

我正在制作一个通用函数,用于在作为公共属性的数组中推送键值对。

当我调用pushDetailsToArray函数时,在getHospitalDetails函数内部,似乎没有填充公共属性数组。尽管当您尝试在pushDetailsArray函数内打印数组时,它会打印出来。有人知道我在这里做错了什么吗?提前谢谢。

public function pushDetailsToArray($row, $array){
        foreach($row as $key => $value){
            $array[$key] = $value;
        }
                    //print_r($this->hospDetails);
        return $array;
    }
    public function getHospDetails(){
        $row = $this->queryThis( "SELECT * from tblhospitals WHERE HospID = '$this->sessionId'" );
        /*foreach($row as $key => $value){
            $this->hospDetails[$key] = $value;
        }*/
        $this->pushDetailsToArray($row, $this->hospDetails);
        print_r($this->hospDetails);

    }

顺便说一句,在getHospitalDetails方法中注释掉的foreach循环可以工作。我只想能够制作一个通用的方法来循环。

也许我错过了什么,但你为什么不这么做呢:

选项1

public function getHospDetails(){
    $row = $this->queryThis( "SELECT * from tblhospitals WHERE HospID = '$this->sessionId'" );
    $this->hospDetails = $row;
}

除非$row不是数组,否则执行选项2。

选项2

public function pushDetailsToArray($row) {
    $output = array();
    foreach($row as $key => $value){
        $output[$key] = $value;
    }
    return $output;
}
public function getHospDetails() {
    $row = $this->queryThis( "SELECT * from tblhospitals WHERE HospID = '$this->sessionId'" );
    $this->hospDetails = $this->pushDetailsToArray($row);
}

另一种方式

public function pushRowToHospDetails($row) {
    foreach($row as $key => $value){
        $this->hospDetails[$key] = $value;
    }
    return $this
}
public function getHospDetails() {
    $row = $this->queryThis( "SELECT * from tblhospitals WHERE HospID = '$this->sessionId'" );
    $this->pushRowToHospDetails($row);
}
public function pushDetailsToArray($row){
        $array=array();
        foreach($row as $key => $value){
            $array[$key] = $value;
        }
        //print_r($this->hospDetails);
        return $array;
    }
    public function getHospDetails(){
        $row = $this->queryThis( "SELECT * from tblhospitals WHERE HospID = '$this->sessionId'" );
        /*foreach($row as $key => $value){
            $this->hospDetails[$key] = $value;
        }*/
         $this->hospDetails=$this->pushDetailsToArray($row);
        print_r($this->hospDetails);

    }

如果您确定用户参考,您可以将pushDetailsToArray更改为:

 <?php
 public function pushDetailsToArray($row, &$array)
 {
     foreach($row as $key => $value)
     {
         $array[$key] = $value;
     }
 }

然后这样称呼它(phpversion>=5.3)

 $this->pushDetailsToArray($row, $this->hospDetails);

或(php版本<5.3):

 $this->pushDetailsToArray($row, &$this->hospDetails);

-

         ********** But, I suggest not to use `reference` **************

您可以简单地将pushDetailsToArray更改为:

 <?php
 public function pushDetailsToArray($row)
 {
     $array = array();
     foreach($row as $key => $value)
     {
         $array[$key] = $value;
     }
     return $array;
 }

然后:

 $this->hospDetails = $this->pushDetailsToArray($row);