输出返回和print_r之间​​的区别

时间:2011-10-26 20:47:13

标签: php codeigniter

这是codeigniter中的控制器,当运行函数get_reunits()时,我在函数marge_orderfun()上有问题。当我在输出(get_reunits())中使用print_r运行函数print_r($hotel_data);时,我给出以下输出(这就是我想要的):

Array (
[0] => Array (
[name] => 11
[price] => 77192276
[extra] => 11
[hotel_id] => 77192276
)
[1] => Array (
[name] => 11
[price] => 77192276
[extra] => 11
[hotel_id] => 77192276
)
) Array (
[0] => Array (
[name] => 666666666
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[1] => Array (
[name] => 99999
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[2] => Array (
[name] => 777777
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[3] => Array (
[name] => 1221
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
) Array (
[0] => Array (
[name] => 666666666
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[1] => Array (
[name] => 99999
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[2] => Array (
[name] => 777777
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[3] => Array (
[name] => 1221
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
)

return $hotel_data;使用输出和运行函数marge_orderfun()时,我得到此输出:

  

{ “reunits”:[{ “名称”: “11”, “价格”: “77192276”, “额外的”: “11”, “HOTEL_ID”: “77192276”},{ “名称”:“11 ”, “价格”: “77192276”, “额外的”: “11”, “HOTEL_ID”: “77192276”}]}

这是我的控制者:

function get_reunits(){
    //$tourf_id   = $this->input->post('tour_name');
    $tourf_id = '102';
    $query_r = $this->db->order_by('id','desc')->get_where('tour_foreign_residence', array('relation' => $tourf_id));                
    foreach($query_r->result() as $idx=>$val){
        $hotel_id = $val->hotel_id;
        $query = $this->db->get_where('tour_foreign_units', array('hotel_id' => $hotel_id));
        if($query->num_rows() > 0){
            $hotel_data = array();
            foreach ($query->result() as $index=>$row) {
                $hotel_data[] = array(
                    'name' => $row->name,
                    'price' => $row->price,
                    'extra' => $row->extra,
                    'hotel_id' => $row->hotel_id
                );                            
            }
        }else{
                return 0;
        }
        //print_r($hotel_data);
        return $hotel_data;
    }
}
function marge_orderfun(){
    //$guide = array('guide' => $this->get_gr());
    //$residence = array('residence' => $this->get_residence());
    $reunits = array('reunits' => $this->get_reunits());
    echo json_encode(array_merge(/*$guide, $residence,*/$reunits));
}

我如何为marge_orderfun()中的输出作为get_reunits()中print_r的输出做什么?

2 个答案:

答案 0 :(得分:1)

实际上,你输出了两个不同的东西。 print_r以一种可读的格式倾倒出PHP数组的结构/内容。你的marge_orderfun会转储一串恰好是JSON格式的文本。这个json字符串USED是一个PHP数组,但现在它只是一个文本blob。

答案 1 :(得分:0)

也许这段代码有帮助:

$a = array();
$b = print_r($a, true);
echo $b;