title: Group by a value of an object, Laravel
body: <p>I have an array of objects, and I want to group by "round_number". </p>
  
  <pre><code>$data = AnswerHistory::select(
      'answer_histories.round_number',
      'answer_histories.answer_time',
      'questions.free_text as question_free_text',
      'lkp_answers.name as answer_name',
      'paragraphs.free_text as paragraph_free_text',
      )
      -&gt;orderBy('answer_histories.id', 'DESC')
      -&gt;where('answer_histories.user_id', 1)
      -&gt;whereNotNull('answer_histories.lkp_answer_id')
      -&gt;leftJoin('questions','answer_histories.question_id','questions.id')
      -&gt;leftJoin('lkp_answers','answer_histories.lkp_answer_id','lkp_answers.id')
      -&gt;leftJoin('paragraphs','answer_histories.paragraph_id','paragraphs.id')
      -&gt;get()-&gt;toArray();
  
  
       //I tried this, but I get only 1 value for each round_number
       $new_array = array();
       foreach ($data as $item)
        if (!array_key_exists($item['round_number'], $new_array))
           $new_array[$item['round_number']] = $item;
  </code></pre>
  
  <p><strong>This is the output:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/HEqOE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HEqOE.png" alt="enter image description here"></a></p>