在数组中仅显示一个重复值但显示所有其他值

时间:2017-10-20 12:12:29

标签: php arrays foreach filter-var

我可以在foreach数组中只显示一个具有多个相同值的值,而不首先在查询中对数组进行分组,如下所示:

0 => 
    array (size=10)
      'id' => string '1' (length=1)
      'questionname' => string 'question 01' (length=36)
      'answerspossible' => 
        array (size=3)
          0 => 
            array (size=2)
              ...
          1 => 
            array (size=2)
              ...
          2 => 
            array (size=2)
              ...
      'answer' => string 'YES' (length=3)
      'answer2' => string '' (length=0)
  1 => 
    array (size=10)
      'id' => string '1' (length=1)
      'questionname' => string 'question 01' (length=36)
      'answerspossible' => 
        array (size=3)
          0 => 
            array (size=2)
              ...
          1 => 
            array (size=2)
              ...
          2 => 
            array (size=2)
              ...
      'answer' => string 'YES' (length=3)
      'answer2' => string 'test answer' (length=0)

我希望视图中的结果是按照foreach中的问题进行分组:

question 01 :
          - answer & answer 2 
          - answer & answer 2

我的代码是:

foreach ($Questions as $Key => $Question) {

        echo $question['questionname'];
        echo $Question['answer']." & ".$Question['answer2'];

}

thnx求助:)

2 个答案:

答案 0 :(得分:0)

$justblank = ''; // just a blank variable we will use it later.
foreach ($Questions as $Key => $Question) {
        echo $question['questionname'];
        $questionanswers = $Question['answer']." & ".$Question['answer2'];
        if($justblank == $questionanswers){
            break;
        }else{
            echo $questionanswers;
        }
        $justblank .= $questionanswers;
}

嗨穆罕默德,我希望这可以帮助你:)。

答案 1 :(得分:0)

空白的var work

$justblank = -1; 
foreach ($Questions as $Key => $Question) {
   if($Question['id']!=$justblank){
    echo $Question['questionname'];
    $justblank=$Question['id'];

   }
     ....
}