0

I have an array which is getting indexed and then storing the values and every time $aba changes it stores ithe values in a new index of arrray $abc[] .what i want is to get an array in a single dimension not geting getting nested
My Array

array (size=3)
   0 => //get rid of this
       array (size=3)
      0 => 
        object(stdClass)[29]
        public 'id' => string '11' (length=2)
        public 'fname' => string 'Tester 1' (length=8)
      1 => 
        object(stdClass)[30]
        public 'id' => string '11' (length=2)
        public 'fname' => string 'Tester 2' (length=8)
  1 => //get rid of this
     array (size=1)
       0 => 
         object(stdClass)[32]
           public 'id' => string '11' (length=2)
        public 'fname' => string 'Tester 3' (length=8)

My Code

$aba= explode('/-', $this->session->userdata('area')); //grabs session data

for($i=1;$i<count($aba);$i++)

  {
          echo $aba[$i];
        $abc[]=$this->db->where('area', $aba[$i])->get('student')->result();

     }  

 var_dump($abc);

I Want the array to be like this

 0 => 
            object(stdClass)[29]
            public 'id' => string '11' (length=2)
            public 'fname' => string 'Tester 1' (length=8)
 1 => 
            object(stdClass)[30]
            public 'id' => string '11' (length=2)
            public 'fname' => string 'Tester 2' (length=8)
  2 => 
                object(stdClass)[30]
                public 'id' => string '11' (length=2)
                public 'fname' => string 'Tester 3' (length=8)
1
  • It Is getting indexed like that because of $abc[] in my for loop and i cant just use $abc as it will keep replacing values Commented Aug 9, 2016 at 4:44

2 Answers 2

1

As i thought it is assigning an array at each key because your query is returning an array. If you want a single array then you can use -

$result = $this->db->where('area', $aba[$i])->get('student')->result();
$abc = array_merge($abc, $result);

It will provide you a single array.

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

1 Comment

@jitendrapurohit answer is a little more clear as he defined the array.. but i upped your post too :)
1

Use array_merge()

$newArr = array();
for($i=1;$i<count($aba);$i++)

  {
      $abc = $this->db->where('area', $aba[$i])->get('student')->result();
      // merge new values into the array instead of adding new keys
      $newArr = array_merge($newArr, $abc)
 }  

 var_dump($newArr); //this should be required array

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.