Reuben Gomes Reuben Gomes - 3 months ago 12
PHP Question

array getting indexed want single key


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)

Answer

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