Codeigniter - 链接几个查询结果

时间:2016-09-01 09:57:40

标签: php codeigniter

我正在使用codeigniter框架工作创建一个api。

我设法为各种api调用创建了一组响应。

public function test1($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

public function test2($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

所以当我单独运行它们时,它会恢复正常。

例如.. http://localhost/codeigniter/index.php/api/test1/sw1

我的主要问题是尝试将这些不同的api调用链接到一个主api调用。就像这样。

public function master($postcode){
    //run a query

    $response = array(
        $this->test1($postcode),
        $this->test2($postcode)
    );

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

但是当我打电话给这个主人api 例如.. http://localhost/codeigniter/index.php/api/master/sw1

我得到了一个奇怪的结果 - 我得到了两个json响应 - 但随后是一个空数组

{
    "similar-property-avg-sold": [
        [{
            "average": "651164.042021172"
        }]
    ]
} {
    "avg-property-sold-growth": {
        "data": [{
            "year": "2011",
            "average": "448696.91018672835"
        }, {
            "year": "2016",
            "average": "651164.042021172"
        }],
        "difference": 145.12336217118
    }
}
Array([0] => [1] => )

1 个答案:

答案 0 :(得分:2)

您没有从前两个子方法返回响应...因此主输出将为null ...

public function test1($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response

    //return response
    return $response;
}

public function test2($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response

    //return response
    return $response;
}