如何使用php在foreach循环中对下一个值进行排序?

时间:2013-05-14 09:13:01

标签: php

我是PHP新手。我想要做的是在foreach循环中隐藏相同的数据。

这是我的代码:

foreach($recording_record as $row ){
echo $row->criteria_title;
echo $row->question_title;
}

我当前的界面: enter image description here

我希望我的界面如何:

enter image description here

这是我的print_r数据:

Array ( [0] => stdClass Object ( [record_id] => 6 [qm_id] => 2 [qm_title] => QM LEAD 2 [record_filename] => abcdefgh.mp3 [userid] => 1002 [unique_id] => 1325554791.71044 [supervisor_id] => 1000 [date_created] => 2013-05-14 05:29:24 [status] => 3 [id] => 6 [callid] => 1325554791.71044 [callerid] => 0383153394 [queuename] => t1-BM_OE [extension] => SIP/1003 [ivrtime] => 2013-05-14 11:14:36 [queuetime] => 2012-01-03 09:40:33 [connecttime] => 2013-05-09 09:40:36 [disconnecttime] => 2012-01-03 09:46:34 [callduration] => 358 [holdduration] => 0 [queueduration] => 3 [ansduration] => 3 [ringduration] => 0 [lastevent] => COMPLETEAGENT [transfer] => [wrapups] => ,12 [tenantid] => 1 [supervisor] => 1005 [username] => Fara [userpass] => 1234 [lastname] => Binti Johari [firstname] => Nurul Farahhin [userlevel] => 2 [usercreated] => 2011-03-26 23:11:56 [lastlogin] => 2012-10-13 20:17:10 [userexten] => [sessionid] => [userstatusid] => 0 [apptype] => webagent [statustimestamp] => 2012-10-13 20:33:21 [lastuserstatusid] => 1 [pqueuetimeout] => 0 [queueroutetype] => 1 [queueroutevalue] => 4 [userdbstatus] => A [criteria_id] => 1 [criteria_title] => Did not demonstrate inappropriate behavior - Zero Tolerance Policy [criteria_rate] => 40 [question_id] => 4 [question_title] => Question 2 [question_type] => n [question_score_y_yes] => [question_score_y_no] => [question_score_y_answer] => [question_score_y] => [question_score_n_a] => 1 [question_score_n_a_value] => True [question_score_n_b] => 0 [question_score_n_b_value] => False [question_score_n_c] => 0 [question_score_n_c_value] => [question_score_n_d] => 0 [question_score_n_d_value] => [question_score_n_e] => 0 [question_score_n_e_value] => [question_score_n_answer] => [question_score_n] => ) [1] => stdClass Object ( [record_id] => 6 [qm_id] => 2 [qm_title] => QM LEAD 2 [record_filename] => abcdefgh.mp3 [userid] => 1002 [unique_id] => 1325554791.71044 [supervisor_id] => 1000 [date_created] => 2013-05-14 05:29:24 [status] => 3 [id] => 6 [callid] => 1325554791.71044 [callerid] => 0383153394 [queuename] => t1-BM_OE [extension] => SIP/1003 [ivrtime] => 2013-05-14 11:14:36 [queuetime] => 2012-01-03 09:40:33 [connecttime] => 2013-05-09 09:40:36 [disconnecttime] => 2012-01-03 09:46:34 [callduration] => 358 [holdduration] => 0 [queueduration] => 3 [ansduration] => 3 [ringduration] => 0 [lastevent] => COMPLETEAGENT [transfer] => [wrapups] => ,12 [tenantid] => 1 [supervisor] => 1005 [username] => Fara [userpass] => 1234 [lastname] => Binti Johari [firstname] => Nurul Farahhin [userlevel] => 2 [usercreated] => 2011-03-26 23:11:56 [lastlogin] => 2012-10-13 20:17:10 [userexten] => [sessionid] => [userstatusid] => 0 [apptype] => webagent [statustimestamp] => 2012-10-13 20:33:21 [lastuserstatusid] => 1 [pqueuetimeout] => 0 [queueroutetype] => 1 [queueroutevalue] => 4 [userdbstatus] => A [criteria_id] => 1 [criteria_title] => Did not demonstrate inappropriate behavior - Zero Tolerance Policy [criteria_rate] => 40 [question_id] => 3 [question_title] => Question 1 [question_type] => y [question_score_y_yes] => 1 [question_score_y_no] => 0 [question_score_y_answer] => [question_score_y] => [question_score_n_a] => [question_score_n_a_value] => [question_score_n_b] => [question_score_n_b_value] => [question_score_n_c] => [question_score_n_c_value] => [question_score_n_d] => [question_score_n_d_value] => [question_score_n_e] => [question_score_n_e_value] => [question_score_n_answer] => [question_score_n] => )

知道该怎么做吗?

4 个答案:

答案 0 :(得分:0)

使用此

 $new_recording_record = array_unique($recording_record);

答案 1 :(得分:0)

检查这是否有效?

$i = 1;
foreach($recording_record as $row ){
    $title = $row->criteria_title;
    $question_title = $row->question_title;

    if($ငငငi==1)
       echo $title;

    echo $question_title;  

    $i++;
}

答案 2 :(得分:0)

试试这个:

echo $recording_record[0]->criteria_title;
foreach($recording_record as $row ){

echo $row->question_title;
}

答案 3 :(得分:0)

$i = 0;
$criteria = array();
foreach($recording_record as $row ){

  $criteria[$i] = $row->criteria_title;
  if($i > 0){
      if ($criteria[$i] != $criteria[$i-1]){
         echo $criteria[$i];
       }
   } else {
        echo $criteria[$i];
   }

  echo $row->question_title;
  $i++;
}