如何基于 get() 雄辩数组进行过滤

Posted

技术标签:

【中文标题】如何基于 get() 雄辩数组进行过滤【英文标题】:How to Filter based on get() eloquent array 【发布时间】:2022-01-12 12:47:58 【问题描述】:

我有以下数组,我想在刀片文件中显示它

索引为 0 的数据应该在不同的表中,而从索引 1 开始的其余数据必须在另一个表中,如下所示;

Subject Name Subject Average
English 40%
Geography 60%
History 70%
Literature 40%
Array (
 [0] => stdClass Object 
 ( [name] => George
    [student_id] => 238
    [lastname] => Smith 
    [middlename] => John
    [grade_name] => Grade 2A
    [stream_name] => Grade 2 
    [section_name] => Junior 
    [student_average] => 69
    [number_of_passed_subjects] => 8 
    [passing_subject_status] => 1
  )

[1] => stdClass Object 
( [student_average] => 56
[subject_name] => English In Literature 
[student_id] => 238 
)

[2] => stdClass Object ( [student_average] => 65 [subject_name] => Geography [student_id] => 238 )
[3] => stdClass Object ( [student_average] => 64 [subject_name] => Mathametics [student_id] => 238 )
[4] => stdClass Object ( [student_average] => 74 [subject_name] => SiSwati [student_id] => 238 )
[5] => stdClass Object ( [student_average] => 66 [subject_name] => Science [student_id] => 238 ) 
[6] => stdClass Object ( [student_average] => 80 [subject_name] => Home Economics [student_id] => 238 )
[7] => stdClass Object ( [student_average] => 45 [subject_name] => Additional Mathematics [student_id] => 238 )
[8] => stdClass Object ( [student_average] => 82 [subject_name] => Religious Education [student_id] => 238 )
[9] => stdClass Object ( [student_average] => 72 [subject_name] => History [student_id] => 238 ) 

)

如何在刀片中做到这一点?

我尝试过使用 foreach,但它只适用于索引 0 中的数据

【问题讨论】:

【参考方案1】:
<table>
  <tr>
    <th> Subject Name </th>
    <th> Subject Average </th>
  </tr>
  @foreach($array as $index=>$item)
    @if($index != 0)
      <tr>
         <td>  $item->subject_name  </td>
         <td>  $item->subject_average  </td>
      </tr>
    @endif
  @endforeach
</table>

【讨论】:

以上是关于如何基于 get() 雄辩数组进行过滤的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 7 雄辩的嵌套条件,用于使用枢轴进行过滤

如何过滤数组中不需要的值以进行绘图?使用 numpy 数组的 matplotlib 中的 ValueError

Laravel - 将数组转换为雄辩的集合

Angular2使用管道基于对象数组过滤对象数组

雄辩的ORM laravel 5获取ID数组

如何过滤数组中不需要的值以进行绘图?使用numpy数组的matplotlib中的ValueError