2017-07-05 27 views
0

如何在PHP中的foreach循環中合併數組?如何在foreach循環中合併數組?

這裏是我的代碼:

$subjects = $this->db->order_by('id','desc')->get_where('tbl_class_management', array('status'=>'1','teacher_id'=>$this->teacher->id))->result(); 
foreach ($subjects as $key => $s) { 
    $std = $this->db->get_where('tbl_student', array('batch'=>$s->batch,'semester'=>$s->semester,'faculty'=>$s->faculty))->result(); 
debug($std); 
} 
+0

要合併哪個數組? –

+0

您是否想要將所有數據存儲在$ std數組中? –

+0

要合併的數組在哪裏= ???? – scaisEdge

回答

1
如果你想通過遍歷 $subjects陣列在 $std陣列中的所有數據存儲

,聲明你的$ STD陣列外循環,然後sipmly一個推在這一個數據當你迭代循環,這樣使用

$subjects = $this->db->order_by('id','desc')->get_where('tbl_class_management', array('status'=>'1','teacher_id'=>$this->teacher->id))->result(); 
$std=[]; 
foreach ($subjects as $key => $s) { 
    $std[] = $this->db->get_where('tbl_student', array('batch'=>$s->batch,'semester'=>$s->semester,'faculty'=>$s->faculty))->result(); 
} 
$std = json_decode(json_encode($std,true),true); 
print_r($std); 
+0

這次你打我吧:) :) – RiggsFolly

+0

@RiggsFolly是的,下次更好運氣的兄弟:-) –

+0

@RAUSHANKUMAR這不行, https://prnt.sc/fs1p76 –

0

用於合併兩個數組的一般公式如下(合併$ array_m到$ array_o):

foreach($array_m as $key=>$value){ 
    $array_o[$key] = $value; 
} 

$ array_o現在將包含所有的$ array_m

編輯的內容:我剛剛注意到在您的文章,你似乎想用array_merge功能。您還可以執行以下操作:

$array_o = array_merge($array_o, array_m);