PHP Codeigniter:$data变量可在print_r中查看,但直接访问会提供';未定义索引';错


PHP Codeigniter: $data variables available to view in print_r but direct access gives 'undefined index' error

有关控制器代码,请参阅最后一个代码块。当我在视图中使用下面的行时,它会返回属性数组中的所有变量:

<?php echo print_r($property);?>

然而,当我只想访问$property数组中的一个项时,我会得到一个未定义的索引错误:

<?php echo print_r($property['county']);?> 

这是var_dump($property)

array (size=1)
  0 => 
    array (size=6)
      'property_id' => string '1' (length=1)
      'address1' => string '26 College Green Walk' (length=21)
      'town_city' => string 'Derby' (length=5)
      'county' => string 'Derbyshire' (length=10)
      'property_description' => string '<p>
    This is a property description for a rental property in Mickleover. Very nice it is too!</p>
' (length=100)
      'property_images_filepath' => string 'ee736-6.jpg' (length=11) 

因为我被难住了,有人能指出去哪里看吗!?

控制器:

public function __construct()
{
    parent::__construct();
    $this->load->model('agency_model');

    $this->output->enable_profiler(TRUE);
}

/**
 * Shows the details of the property specified by the $id parameter
 * @param string $id the id of the property to be retrieved from the DB
 */
public function show () {

$id = $this->input->get('property_id');

    if (!isset($id)) 
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    else
    {
        $data['property'] = $this->agency_model->get_property_details($id);
        $data['title'] = 'Current Properties';
        $this->load->view('templates/header', $data);
        $this->load->view('agency/property_details_view', $data);
        $this->load->view('templates/footer');
    }
}

}

谢谢Gareth

试试这个。如果你的阵列在控制器中是这样的

$data['info'] = array(
    'col_1' => "first_1",
     'col_2'=>  "first_2"
);
$this->load->view('folder/page',$data);

那么在视图中,您可以这样访问它。

echo $info['col_1'];
echo $info['col_1'];

或者,如果是多个记录的数组,您将使用

foreach($info as $row)
{
 //your code/statements.
}

我认为这是语法错误,你的意思是:

<?php echo print_r($property['country');?>  

而不是:

<?php echo print_r($property['county');?>  

我怀疑这会给你想要的

 foreach ($property as $properties)
     print_r($properties['county']);
 endforeach;

这很容易错过,但以下内容会起作用。

<?php echo print_r( $property[ 0 ]['county'] ); ?> 

当您执行var_dump时,我注意到有一个数组索引。