我是Laravel的绝对初学者,甚至是php。
我想删除下面的数组元素。
英语不是我的第一语言,所以如果这个问题对您没有意义,请留下您的意见。任何意见,将不胜感激!提前谢谢!
myController的
public function deleteLog(Course $course, CreateCourseRequest $request){
$courseWeeks = $course->weeks;
$courseWeeksCount = intval($courseWeeks);
$inputWeeks = $request->input('weeks');
$inputWeeksCount = intval($inputWeeks);
$inputUsers = $request->input('first_name_list');
for ($i = $courseWeeksCount; $i >= $inputWeeksCount; $i--) {
foreach($inputUsers as $user) {
/* I need to write code that deletes certain numbers of elements of array.
For example, when the admin changes the weeks of the course 'intro to php'
from 5 to 3, logs whose number of weeks are 4 and 5 will be deleted */
}
}
}
基本上,我想与下面的addLog函数相反。
public function addLog(Course $course, CreateCourseRequest $request){
$courseWeeks = $course->weeks;
$courseWeeksCount = intval($courseWeeks);
$inputWeeks = $request->input('weeks');
$inputWeeksCount = intval($inputWeeks);
$inputUsers = $request->input('first_name_list');
$logs = array();
for ($i = $courseWeeksCount; $i <= $inputWeeksCount; $i++) {
foreach($inputUsers as $id) {
array_push($logs, array(
'user_id' => $id,
'course_id' => $course->id,
'weeks' => $i,
'work_description' => '',
'instructor_comments' => '',
'status' => 'accepted',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
));
}
}
Log::insert($logs);
}
在MyController中,有以下功能。
if($this->AreInputWeeksLess($course, $request)){
//$this->deleteLog($course, $request);
}
public function AreInputWeeksLess(Course $course, CreateCourseRequest $request){
$courseWeeks = $course->weeks;
$courseWeeksCount = intval($courseWeeks);
$inputWeeks = $request->input('weeks');
$inputWeeksCount = intval($inputWeeks);
if($inputWeeksCount - $courseWeeksCount < 0){
return true;
} else{
return false;
}
}
Course_Edit.blade.php
<div class = "form-group">
{!! Form::label('weeks', 'Weeks:') !!}
{!! Form::selectRange('weeks', 1, 17) !!}
</div>
<div class = "form-group">
{!! Form::label('first_name_list', 'Students:') !!}
{!! Form::select('first_name_list[]', $students, null, ['id' => 'first_name_list' ,'class' => 'form-control', 'multiple']) !!}
</div>
答案 0 :(得分:1)
在许多选项中,我建议使用两种非常基本的解决方案,这些解决方案不是很干净,但很简单且很少:
例如:
$fruitArray = [
['fruit' => 'apple',
'fruitId' => 'appleId',
'price' => 10
],
['fruit' => 'banana',
'fruitId' => 'bananaId',
'price' => 10
],
['fruit' => 'mango',
'fruitId' => 'mangoId',
'price' => 10
]
]; // this is your array structure
$collection = \Illuminate\Support\Collection::make($fruitArray);
$filteredCollection = $collection->reject(function ($item) {
if (array_get($item, 'fruit') === 'mangoId') {
return false;
} else {
return true;
}
}); // using callback function call we saying reject item from collection. Function callback decides what stays what not. If call back returns false item is removed from collection.
// Next step is unecessary but if you comfortable with arrays only convert your collection back to array.
$fruitArray = $collection->toArray();
$logs[] =
[
'user_id' => 1,
'course_id' => 1,
'weeks' => 1,
'work_description' => '',
'instructor_comments' => '',
'status' => 'accepted',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
// you don't even need those two horrible for loops instead do something like below
$logs = array_map(function ($item) use ($courseWeekCount, $inputCourseWeek) {
if ($inputCourseWeek < $courseWeekCount) {
if (array_get('$item', 'week') <= $inputCourseWeek) {
return $item;
} else {
return [];
}
} else {
return $item;
}
}, $logs);