0

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

当我调用 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 循环有效。我只想能够制作一个通用的循环方法。

4

3 回答 3

0

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

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

然后像这样调用它(phpversion> = 5.3)

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

或(phpversion < 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);
于 2013-07-24T16:59:49.073 回答
0
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);


    }
于 2013-07-24T17:03:23.273 回答
0

也许我错过了一些东西,但你为什么不这样做:

选项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);
}
于 2013-07-24T17:04:01.670 回答