-2

我有一个来自 mysql Query 的结果集,查询类似于SELECT DISTINCT c.id as course_id, c.fullname as fullname, c.shortname as shortname,它返回为$return[],我想做的是c.id从这个结果集中获取一个并将其存储在数组中。我有

foreach($returnSet as $rs)
{
    $i = 0;
    $courses=array($course->course_id[$i]);
    $i++;
}

这是正确的方法吗,还是我走错了路,有人指导吗?)

4

3 回答 3

2

它应该如下所示

$cources = array();
foreach($returnSet as $rs)
{
     $courses[] = $rs->course_id;
}
于 2013-04-04T12:01:47.507 回答
0

我不确定我是否正确理解了您的问题,但我认为答案是:

$array = array();

$query = mysql_query("SELECT DISTINCT c.id as course_id, c.fullname as fullname, c.shortname as shortname") or die(mysql_error());
while($data = mysql_fetch_assoc($query)){
  $array[] = $data["course_id"];
}

print_r($array); #=> array(1, 2, 3, 4...)
于 2013-04-04T12:02:22.903 回答
0

你可以这样做:

$courses = array();
if (!empty($returnSet)) { //check if the $returnSet has anything inside it
   foreach($returnSet as $rs) {
      $courses[] = isset($rs->course_id) ? $rs->course_id : '';
   }
}

print_r($courses);

希望这可以帮助 :)

于 2013-04-04T12:04:09.113 回答