我有一个数组,当我以这种方式打印时,其值为
Array (
[q1] => Array (
[0] => stdClass Object (
[student_unique_id] => 6
[studentname] => studentname
[studentpassword] => 1213
[dob] => 09/05/16
[studentenrollmentnumber] => 1341243124
[studentcontactnumber] => 9460930479
[studentemailid] => abhisehk@mail.com
[studentdepartmentname] => department of agriculture
[studentpasswordtstatus] => 0
)
)
[q2] => Array ( )
)
当我使用代码print_r($prar);
时,这些数组的所有值都来自数据库而我正在使用mvc框架codeigniter。现在我需要使用key.Sorry将数组拆分为2个新数组,如果它听起来很愚蠢但我是新来的!
答案 0 :(得分:1)
注意:当您使用print_r()
时,当您从数据库中提取值时,它会在CI中输出stdClass Object
的数组。
有一种解决方案可以在迭代时不使用stdClass Object
来显示数组。
示例:考虑$final
考虑数组,在使用print_r()
时,它会显示stdClass Object
。
stdClass Object
。<强>代码:强>
您可以使用此代码将其用于使用Controller和Model从数据库中检索的值。
如果从DB中检索单行输出
<?php
foreach($final->result() as $single)
{
//You can print the variable values over here as follows (E.g) echo $single->id
}
?>
如果从DB中检索到多行输出
<?php
$row=array();
foreach($final->result() as $single)
{
//You can store it as an array here if you are going on with multiple loops
$row[] = $single;
}
print_r($row); // here you can save it as an another array
?>
如果您使用上述方法检索输出,那么您的模型应该是这样的示例。
<强> employee_model.php 强>
<?php
class Employee_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getEmployees()
{
$this->db->select('*');
$this->db->from('employee');
$this->db->where('delete_status','0');
$this->db->where('status','1');
$this->db->order_by('id','DESC');
$query = $this->db->get();
return $query;
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Layout_Controller {
public function __construct(){
parent::__construct();
$this->load->library("pagination");
$this->load->model('employee_model');
$this->load->model('ajax_model');
}
public function employee_listing()
{
$result['all_employee'] = $this->employee_model->getEmployees(); // getEmployees is the function name in the employee_model
$this->load->view('frontend/employee_list',$result);
}
答案 1 :(得分:1)
你可以尝试下面的代码:
$new_array = array();
foreach($prar as $key=>$val) {
$new_array[] = $val;
}
echo "<pre>";
print_r($new_array);
或者extract()的一个可能用途是导入wddx_deserialize()返回的关联数组中包含的符号表变量。
extract($prar, EXTR_PREFIX_SAME, "wddx");
echo "<pre>";
print_r($q1);
echo "</pre>";
echo "<pre>";
print_r($q2);
echo "</pre>";
以上示例将输出:
[0] => stdClass Object (
[student_unique_id] => 6
[studentname] => studentname
[studentpassword] => 1213
[dob] => 09/05/16
[studentenrollmentnumber] => 1341243124
[studentcontactnumber] => 9460930479
[studentemailid] => abhisehk@mail.com
[studentdepartmentname] => department of agriculture
[studentpasswordtstatus] => 0
)
Array ( )