2

Good day everyone, I have this function that can generate time interval and store them to $time.

if(strtotime($startTime) <= strtotime($endTime))
        {
          $this->time[$i]['room'] = '49';
          $this->time[$i]['day'] = 'T-Th';
          $this->time[$i]['c_time'] = $start.'-'.$end;
          $this->time[$i]['sy'] = '2021-2022';
          $this->time[$i]['sem'] = '1st';
        }

Sample output of $time is like this

1 => array:5 [▼
    "room" => "49"
    "day" => "T-Th"
    "c_time" => "07:00-08:30"
    "sy" => "2021-2022"
    "sem" => "1st"
  ]
  2 => array:5 [▼
    "room" => "49"
    "day" => "T-Th"
    "c_time" => "08:30-10:00"
    "sy" => "2021-2022"
    "sem" => "1st"
  ]
  3 => array:5 [▼
    "room" => "49"
    "day" => "T-Th"
    "c_time" => "10:00-11:30"
    "sy" => "2021-2022"
    "sem" => "1st"
  ]]

What should I do so that the output would be a collection->toArray() just like this

array:5 [▼
  0 => {#1416 ▼
    +"room": "49"
    +"day": "M-W"
    +"c_time": "13:00-14:00"
    +"sy": "2021-2022"
    +"sem": "1st"
  }
  1 => {#1435 ▼
    +"room": "49"
    +"day": "M-W"
    +"c_time": "11:30-13:00"
    +"sy": "2021-2022"
    +"sem": "1st"
  }
  2 => {#1433 ▼
    +"room": "49"
    +"day": "M-W"
    +"c_time": "13:00-14:30"
    +"sy": "2021-2022"
    +"sem": "1st"
  }]
3
  • 2
    What have you tried so far? Where are you stuck? Why not generate a collection in the first place? Commented Feb 1, 2022 at 9:16
  • Yeah, How should i do it so that it would generate a collection? Commented Feb 1, 2022 at 9:22
  • In the docs you can find everyting about collections . laravel.com/docs/8.x/collections#method-push Commented Feb 1, 2022 at 9:45

2 Answers 2

1

Since you did not provide us with where you define the $time variable, I can't help you with any issues there. However, first, instantiate that variable as a collection.

$this->time = collect([]);

And then you can push in the following manner.

 $this->time->put($i, (object) [
    'room' => '49',
    'day' => 'T-Th',
    'c_time' => $start.'-'.$end,
    'sy' => '2021-2022',
    'sem' => '1st',
 ]);
Sign up to request clarification or add additional context in comments.

8 Comments

can I ask something?
You already did :) Go ahead.
Hahahha, from my question what the difference between the first array(top) and the second array(bottom)?
@itsmejoe The first is an array of arrays, and the second is an array of objects. You need to push an object instead of an array to achieve that result.
Okay now I understand, so how should one create an array of objects?
|
1

The simplest way to convert an array to collection is to use Laravel's collect() helper function. It takes an array as a parameter and returns a collection type.

For example if we have this array:

$a = [
       ['name' => "abc", 'age' =>45],
       ['name' => "xyz", 'age' =>20],
     ];
        dd(collect($a));

The output will be:

enter image description here

2 Comments

Very good, but i would suggest to build the collection instead of building the array
Although I appreciate the answer but I am asking how do i construct the $time to return collection, because what i have done so far returns an array of arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.