【发布时间】:2019-09-20 07:30:47
【问题描述】:
代码:
<?php
$this->db->select('*');
$this->db->from('student');
$this->db->order_by('studentID','DESC');
$sql1 = $this->db->get();
$result1 = $sql1->result_array();
foreach($result1 as $arr1)
{
$array1[] = array(
'link' => 'student',
'values' => '<b>'.$arr1['create_username'].'</b> added new student <b>'.$arr1['name'].'</b>',
'dates' => $arr1['s_date']
);
}
$this->db->select('*');
$this->db->from('professor');
$this->db->order_by('professorID','DESC');
$sql2 = $this->db->get();
$result2 = $sql2->result_array();
foreach($result2 as $arr2)
{
$array2[] = array(
'link' => 'professor',
'values' => '<b>'.$arr2['create_username'].'</b> added new professor <b>'.$arr2['name'].'</b>',
'dates' => $arr2['s_date']
);
}
$this->db->select('*');
$this->db->from('classes');
$this->db->order_by('classesID','DESC');
$sql3 = $this->db->get();
$result3 = $sql3->result_array();
foreach($result3 as $arr3)
{
$array3[] = array(
'link' => 'classes',
'values' => '<b>'.$arr3['create_username'].'</b> added new course <b>'.$arr3['name'].'</b>',
'dates' => $arr3['s_date']
);
}
foreach(array_combine($array1,$array2,$array3) as $rowss)
{
echo '<li>
<a href="'.base_url().''.$rowss['link'].'">
<p>'.$rowss['values'].'</p>
</a>
<a href="javascript:void(0)">'.time_elapsed_string($rowss['dates']).'</p>
</li>';
}
在这段代码中,我只是同时对表student, professor and classes 运行三个查询。现在,我想将所有数组(即 foreach 循环内的 array1, array2, array3)合并或映射到一个 array 中。那么,我该如何解决呢?请帮帮我。
谢谢
【问题讨论】:
-
你要找的方法是array_merge php.net/manual/fr/function.array-merge.php
-
这与jQuery无关。请使用适当的标签。
-
array_merge($array1,$array2,....)
标签: php arrays codeigniter