1

Every time, I query data from database table, it returns result as multi-dimensional array, so I have to do nested for-loop in view in order to get an actual data that I want. I don't want it to be like that because it is not necessary to a nested loop every time for my case. Is there any way that I can get only one dimensional array returned that points to actual data? Thanks.

Here's my model:

    public function get_contact_info($userid){
        $res = $this->db->get_where('contact_info',array('userid'=>$userid));
        return $res->result_array();
    }

Here's my controller:

   $userid = $this->session->userdata('userid');

   $this->load->model('User','user');
   $res = $this->user->get_contact_info($userid);

    var_dump($res);

Output:

   C:\wamp64\www\findjob.com\application\controllers\Job.php:64:
    array (size=2)
      0 => 
        array (size=6)
          'contact_id' => string '1' (length=1)
          'name' => string 'Jonh' (length=4)
          'title' => string 'Mr.' (length=3)
          'phone' => string '01932834784' (length=11)
          'email' => string '[email protected]' (length=14)
          'userid' => string '1' (length=1)
      1 => 
        array (size=6)
          'contact_id' => string '2' (length=1)
          'name' => string 'Smith' (length=5)
          'title' => string 'Mr.' (length=3)
          'phone' => string '0293893' (length=7)
          'email' => string '[email protected]' (length=15)
          'userid' => string '1' (length=1)
12
  • Please explain how it would be possible to return 2 result rows each with multiple column data as a single dimensional array?? Oh please, please... tell me, please Commented May 14, 2017 at 15:30
  • @RiggsFolly, i did not quite get it, that's my question. Exactly, I don't want my array to be preceded with index 0 and 1. Commented May 14, 2017 at 15:42
  • And my point! How would you return 2 or more rows any other way Commented May 14, 2017 at 15:44
  • @RiggsFolly, another way is to return result as object example. return $res->result();. Sorry, if it does not hit your point. Commented May 14, 2017 at 15:53
  • 1
    Show an example of what you are trying to avoid. A single foreach shoudl be enough to process this example Commented May 14, 2017 at 16:15

1 Answer 1

1

Since my model return result as an array of multiple rows and columns, there is no way to get data without looping. For instance, my loop as

    foreach($jobs as $key => $value){
      $value['column_name'];
    }

Because $value here is another array containing another key and value of itself, and it is the value that I need to get. Thus, from inside the loop, I used $value['column_name'] to access the value of each column that I need.

By this way, I can avoid using nested foreach loop, a loop through array $value. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

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.