ILoveBaymax
ILoveBaymax

Reputation: 267

Laravel:5.0 How to delete elements in array

I am an absolute beginner of Laravel and even php.

I would like to delete elements of array below.

English is not my first language, so if this question does not make sense to you, please leave you comments. Any advice would be appreciated! Thanks in advance!

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   */
        }
    }
}

Basically, I would like to do the opposite of addLog function below.

 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);
}

In MyController, there are functions below.

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>

Upvotes: 1

Views: 1695

Answers (1)

Perdeep Singh
Perdeep Singh

Reputation: 492

Out of many options I am suggesting two very basic solutions not very clean but easy and few lines:

  1. Convert this array into collection and use collection methods to search and remove item.

E.g.:

$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();
  1. Using finding in array as your were doing:

$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);

Upvotes: 1

Related Questions