Search code examples
phparraysforeachfilter-var

Show duplicate value in array only one but show all others


can i showing only one value in foreach array having multiple same value, without grouping the array firstly in the query like this :

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)

Result i want in the view is to group by the questioname inside the foreach :

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

My code is :

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

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

}

thnx for help :)


Solution

  • $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;
    }
    

    Hi Mohammed, I hope this help you :).