如何从 php 中的关联数组的键创建一个新数组

how to make an new array from keys of an associative array in php

我有一个数组,当我打印出来时它的值是这样的

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.Now 我需要使用 [=18 将数组拆分为 2 个新数组=] 如果这听起来很愚蠢,但我是新来的!

注意:当你使用print_r()时,它会在你拉出一个值时输出stdClass Object in CI的数组来自数据库。

有一个解决方案可以在迭代时不使用 stdClass Object 来显示数组。

示例: 考虑 $final 考虑数组并在使用 print_r() 时显示 stdClass Object

  • 您必须按如下方式使用循环,以便在打印时避免 stdClass Object

代码:

您可以将此代码用于使用控制器和模型从数据库中检索的值。

If single row of the output is retrieved from the DB

<?php
foreach($final->result() as $single)
{
   //You can print the variable values over here as follows (E.g) echo $single->id    
}
?>

If Multiple row of the output is retrieved from the 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
?>

如果您在 foreach 中使用 `->result()` 来获取值,模型代码应该是什么样子

如果您使用上述方法检索输出,您的模型应该如下所示。

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);
 }

你能试试下面的代码吗:

$new_array = array();
foreach($prar as $key=>$val) {
    $new_array[] = $val;
}

echo "<pre>";
print_r($new_array);

或者 extract() 的一个可能用途是将包含在 wddx_deserialize() 返回的关联数组中的变量导入符号 table。

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 ( )