PHP函数获取第二个函数返回的数组

时间:2018-10-10 12:17:45

标签: php arrays function

我有2个功能(位于同一文件中)

  

f1 = getBlogCommentList

     

f2 = getBlogReplyList

我希望函数f1调用f2 $this->getBlogReplyList($post_id,$comment['id']);,f2函数根据f1发送的参数从sql收集数据,并返回包含数据return $blog_replies;的数组。以及我想在f1中使用的返回数据,但无法理解如何获取此返回数据。foreach ($blog_replies as $reply) { //Do stuff with returned data }

  

注意:未定义的变量:第146行的D:\ xampp \ htdocs \ models \ BlogModel.php中的blog_replies

     

警告:第146行的D:\ xampp \ htdocs \ models \ BlogModel.php中为foreach()提供的参数无效

在146行中,我有foreach($ blog_replies为$ replies)

f1函数(getBlogCommentList)

public function getBlogCommentList($post_id){
try{

    $sortby = "bla bla bla";

    $stmt = $this->conn->prepare("$sortby");
    $stmt->execute();
    $result = $stmt->fetchAll();

    $blog_comments = array();

    foreach($result as $comment){
        $blog_comments[] = $comment;
        $this->getBlogReplyList($post_id,$comment['id']);
    }

    foreach ($blog_replies as $reply) {
        //Do stuff with returned data
    }

    return $blog_comments;

  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }
}

f2函数(getBlogReplyLyst)

public function getBlogReplyList($post_id,$comment_id){
try{

    $sortby = "bla bla bla";

    $stmt = $this->conn->prepare("$sortby");
    $stmt->execute();
    $result = $stmt->fetchAll();

    $blog_replies = array();

    foreach($result as $post){
        $blog_replies[] = $post;
    }

    return $blog_replies;

  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }
}

2 个答案:

答案 0 :(得分:2)

您需要将被调用函数的输出分配给变量,因为变量只能在函数中访问,即:

$blog_replies = $this->getBlogReplyList($post_id,$comment['id']);

foreach ($blog_replies as $reply) {
    //Do stuff with returned data
}

您还可以考虑使用类变量,这样您就可以在对象的每个函数中访问该变量。请注意$this->blog_relies的用法,顶部的变量定义和return语句的删除

示例:

class ExampleClass {

  private $blog_replies;

  public function getBlogReplyList($post_id,$comment_id){
    try{

        $sortby = "bla bla bla";

        $stmt = $this->conn->prepare("$sortby");
        $stmt->execute();
        $result = $stmt->fetchAll();

        $this->blog_replies = array();

        foreach($result as $post){
            $this->blog_replies[] = $post;
        }

        // The return is now obsolete
        // return $blog_replies;

      }
      catch(PDOException $e)
      {
        echo $e->getMessage();
      }
   }

  public function getBlogCommentList($post_id){
    try{

      $sortby = "bla bla bla";

      $stmt = $this->conn->prepare("$sortby");
      $stmt->execute();
      $result = $stmt->fetchAll();

      $blog_comments = array();

      foreach($result as $comment){
        $blog_comments[] = $comment;
        $this->getBlogReplyList($post_id,$comment['id']);
      }

      foreach ($this->blog_replies as $reply) {
        //Do stuff with returned data
      }

      return $blog_comments;

    }
    catch(PDOException $e)
    {
      echo $e->getMessage();
    }
  }


}

答案 1 :(得分:0)

如下修改f1:

public function getBlogCommentList($post_id){
    try{

        $sortby = "bla bla bla";

        $stmt = $this->conn->prepare("$sortby");
        $stmt->execute();
        $result = $stmt->fetchAll();

        $blog_comments = array();

        foreach($result as $comment){
            $blog_comments[] = $comment;

            $blog_replies = $this->getBlogReplyList($post_id,$comment['id']);
            foreach ($blog_replies as $reply) {
                //Do stuff with returned data
            }
        }

        return $blog_comments;

    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}