0

I have an array whose value when i print out comes out in these way

Array ( 
        [q1] => Array ( 
                        [0] => stdClass Object (
                            [student_unique_id] => 6 
                            [studentname] => studentname 
                            [studentpassword] => 1213 
                            [dob] => 09/05/16 
                            [studentenrollmentnumber] => 1341243124 
                            [studentcontactnumber] => 9460930479 
                            [studentemailid] => [email protected] 
                            [studentdepartmentname] => department of agriculture 
                            [studentpasswordtstatus] => 0 
                        ) 
                    ) 
        [q2] => Array ( )
)

when i use the code print_r($prar);these all values of array are coming from database and i am using an mvc framework codeigniter.Now i need to split the array into 2 new arrays using key.Sorry if it sounds stupid but i am new here!

6
  • can you split 2 new array using q1 and q2 keys? Commented Oct 7, 2016 at 4:19
  • @razibalmamun sorry but i am novice and i dont have idea which function to use to do so? Commented Oct 7, 2016 at 4:31
  • Can you put here sample output how you want? Commented Oct 7, 2016 at 4:31
  • @VijaysinhParmar i want to make sure the key q1 in array becomes an new array in itself and key q2 becomes an second array in itself! Commented Oct 7, 2016 at 4:33
  • @AbhishekJoshi Please provide your output in comment box what you are want. Commented Oct 7, 2016 at 4:34

2 Answers 2

1

Note: When you use the print_r() it will output the array with stdClass Object in CI when you are pulling out a value from the Database.

There is a solution to display the array without using the stdClass Object while iterating.

Example: Consider $final consider the array and while using print_r() it displayed the stdClass Object.

  • You have to use the loop as follows so that it avoids the stdClass Object while printing it.

Code:

This code you can use it for the values to retrieve from the DB using the Controller and Model.

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
?>

How should the model Code look like if you are using `->result()` in the foreach to get the values

Here is the sample that your model should look like if you are using the above methods for the retrieving of the output.

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

How to call the model from the controller

<?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);
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Can you try bellow code :

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

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

or A possible use for extract() is to import into the symbol table variables contained in an associative array returned by wddx_deserialize().

extract($prar, EXTR_PREFIX_SAME, "wddx");

echo "<pre>";
print_r($q1);
echo "</pre>";

echo "<pre>";
print_r($q2);
echo "</pre>";

The above example will output:

[0] => stdClass Object (
                            [student_unique_id] => 6 
                            [studentname] => studentname 
                            [studentpassword] => 1213 
                            [dob] => 09/05/16 
                            [studentenrollmentnumber] => 1341243124 
                            [studentcontactnumber] => 9460930479 
                            [studentemailid] => [email protected] 
                            [studentdepartmentname] => department of agriculture 
                            [studentpasswordtstatus] => 0 


)

Array ( )

1 Comment

Thanks for the reply. Hope so it is better to use the method which i have suggested since it will be the safest method to get rid of stdClass Object. And it is an extra process to store another array() as it takes more time for the execution of the code. Hope so you could understand better from my explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.