I have the following array of column names, I want to only display the day column names, so 7 - 13
array:20 [▼
0 => "id"
1 => "customer_id"
2 => "enrolment_id"
3 => "course_id"
4 => "delivery_mode"
5 => "course_cost"
6 => "location"
7 => "Monday"
8 => "Tuesday"
9 => "Wednesday"
10 => "Thursday"
11 => "Friday"
12 => "Saturday"
13 => "Sunday"
14 => "start_time"
15 => "end_time"
16 => "start_date"
17 => "sale_type"
18 => "created_at"
19 => "updated_at"
]
Array is created in controller with:
$columns = Schema::getColumnListing('orders');
I'm using the following to display the column names as labels for my checkboxes in a blade template view but it shows all column names and not just the days:
#foreach($columns as $column)
{{ Form::checkbox($column, 0, null, ['id' => $column, 'class' => 'is-
checkradio is-white']) }}
{{ Form::label($column, $column) }}
#endforeach
Thanks Jonas
This worked for the question
$days = array_slice($columns, 7, 7);
You can use the array_where from laravel helpers (https://laravel.com/docs/5.4/helpers#method-array-where) to do this job, like this:
$columns = array_where($columns, function ($value, $key) {
return ($value >= 7 && $columns <= 13);
});
Related
I'm trying to make a holiday array, it is based on work_day.
For example is if in 1 group there's only day 1-2 (sunday-monday), then the other day (tuesday-saturday) is holiday. And based on that days, I want to get a holiday date array from start of the year until now.
I have a table like this:
attendance_group_id | day
GROUP-001 | 1
GROUP-001 | 2
GROUP-002 | 1
GROUP-003 | 1
What I've done first is getting all data from work_day table using code bellow:
foreach ($group_work_hour as $hours) {
foreach ($hours as $hour){
$work_day = (object) array();
$work_day->group_id = $hour->attendance_group_id;
$work_day->day = $hour->day;
$work_day_arr[] = $work_day;
}
}
And the result from code above is like bellow, every group can have day up to 7 day (1-7). 1 for sunday and 7 for saturday.
Array work day
array (
0 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 2,
),
1 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 3,
),
2 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 4,
),
3 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 5,
),
4 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 6,
),
5 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 7,
),
6 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 2,
),
7 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 3,
),
8 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 4,
),
9 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 5,
),
10 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 6,
),
And what I've done next is to get every date from start of the year until today and store it to an array.
$now = \Carbon\Carbon::now()->format('Y-m-d');
$start = \Carbon\Carbon::parse($now)->startOfYear()->format('Y-m-d');
$dateRangePeriod = \Carbon\CarbonPeriod::create($start, $now);
$dateRange = [];
foreach ($dateRangePeriod as $key => $date) {
$dateRange[] = $date->format('Y-m-d');
}
The result from code above is like this:
Array date
array (
0 => '2022-04-01',
1 => '2022-04-02',
2 => '2022-04-03',
3 => '2022-04-04',
4 => '2022-04-05',
5 => '2022-04-06',
6 => '2022-04-07',
7 => '2022-04-08',
8 => '2022-04-09',
9 => '2022-04-10',
10 => '2022-04-11',
11 => '2022-04-12',
12 => '2022-04-13',
)
What I want to do next is, I want to get only holiday date from array date above. But I don't know how to do that because from work_day I only get group_id and the number of day.
How to know if the day from array date is the holiday based on array work_day.
I'm trying some way to doing it, but I can't found the solution yet. I've tried like code bellow, but still not working:
// Loop all date from array date
foreach ($dateRange as $range) {
// Get number of day from date
$day = date('N', strtotime($range));
// Loop work day array
foreach ($work_day_arr as $work) {
// When day from work not equals to day from date (this is a right way to check if days from array date is a holiday)?
if($work->day != $day) {
// Check if group_id & off_date (holiday date) is not set
if(!isset($data['attendance_group_id'], $data['off_date'])) {
$data['attendance_group_id'] = $work->group_id;
$data['off_date'] = date('Y-m-d', strtotime($range));
$objData[] = $data;
}
}
}
}
The example result that I want is something like this (excluding holiday date):
From:
array (
0 => '2022-04-01', // holiday date
1 => '2022-04-02',
2 => '2022-04-03',
3 => '2022-04-04',
4 => '2022-04-05',
5 => '2022-04-06', // holiday date
6 => '2022-04-07',
7 => '2022-04-08',
8 => '2022-04-09',
9 => '2022-04-10',
10 => '2022-04-11',
11 => '2022-04-12', // holiday date
12 => '2022-04-13', // holiday date
)
To (excluding holiday date):
array (
0 => '2022-04-02',
1 => '2022-04-03',
2 => '2022-04-04',
3 => '2022-04-05',
4 => '2022-04-07',
5 => '2022-04-08',
6 => '2022-04-09',
7 => '2022-04-10',
8 => '2022-04-11',
)
Another case example
From array work day:
day = 1, which is Sunday
array date:
01-01-2022
02-02-2022 // if this date == sunday, then this is a holiday
03-02-2022
result:
02-02-2022 // only get holiday date
I have array of days and their open time and close time as below, I need to store these days and the open and close time for a selected store in table, in the same time if the store have already entered in this table then update the current records so no duplicate will be found
I hope you can help me in this
many thanks
"store_id" => "625"
"day_id" => array:7 [
1 => "1"
2 => "2"
3 => "3"
4 => "4"
5 => "5"
6 => "6"
7 => "7"
]
"open_time" => array:7 [
1 => "13:20"
2 => "01:20"
3 => "13:20"
4 => "01:20"
5 => "15:50"
6 => "07:20"
7 => "23:20"
]
"close_time" => array:7 [
1 => "20:20"
2 => "08:20"
3 => "20:20"
4 => "21:20"
5 => "18:20"
6 => "00:20"
7 => "19:20"
]
I tried with this but need more
public function store(Request $request)
{
$store_id=$request->storeinfo_id;
foreach ($request->input('day_id') as $key => $val) {
$array = [
'day_id' => $val,
];
Storeday::where('storeinfo_id',$store_id)->create($array);
}
return response()->json(['data' => trans('message.success')]);
}
I'm trying to sum a value of an array with the same employee no.
Here's the example of the array I'm trying to sum.
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 8
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
and
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 8
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
they are duplicates array in which i want to get the sum of work hours only to have this result
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 16
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
There are about 24 and more occurrences if this array in my result set in which i want to merge as one summing up the total work hours
Tried this one but getting no luck
foreach ($employee_attendance as $row){
if($row['employee_id'] === $row['employee_id']){
$payroll_data['work_hours'] += $row['total_hours'];
}
$payroll[] = $payroll_data;
}
The best would be to get it sorted out from Eloquent queries itself. However, if you wish, you can also use Collections' methods such as groupBy and each to filter the values based on employee ID and do a sum on them. Finally, have a result variable to store all the results.
Snippet:
$result = [];
$arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
$sum = $item->sum('work_hours');
$emp_data = $item->first();
$emp_data['work_hours'] = $sum;
$result[] = $emp_data;
});
Full Code:
<?php
$arr = collect([
[
"employee_no" => "04052018",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 8,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
],
[
"employee_no" => "04052018",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 8,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
],
[
"employee_no" => "04052019",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 80,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
]
]);
$result = [];
$arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
$sum = $item->sum('work_hours');
$emp_data = $item->first();
$emp_data['work_hours'] = $sum;
$result[] = $emp_data;
});
print_r($result);
As what others said, it's better to group and sum those work hours when you retrieve it from the database.
But in case you still want to do it like what you explained above, it can be done like this although it might not be the best way
foreach ($employee_attendance as $row){
// Check if this is the first occurence of the employee number or not
if (!isset ($payroll_data[$row['employee_no']]['work_hours']) ) {
// First occurence will set the total hours to the current loop work hours
$payroll_data[$row['employee_no']]['work_hours'] = $row['work_hours'];
} else {
// Second occurence and so on will sum previous total work hours
$payroll_data[$row['employee_no']]['work_hours'] += $row['work_hours'];
}
}
And the $payroll_data structure will be like this:
[
"04052018" => [
"work_hours" => 16 // Total work hours of this employee number
],
// Repeat for other employee number
]
Hence, it's easier for you to check the employee number with their total working hours as well.
I have api base data like:
array:21 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "pcp"
5 => "rpx"
6 => "cahaya"
7 => "dse"
8 => "first"
9 => "indah"
10 => "jet"
11 => "jnt"
12 => "ncs"
13 => "pahala"
14 => "pandu"
15 => "sap"
16 => "sicepat"
17 => "slis"
18 => "star"
19 => "nss"
20 => "wahana"
]
which i get with this code:
$cori = $rajaongkir->courier('all');
$cori = array_keys($cori);
I also have database table named couriers where i saved same names as you see in my dd above + status and dd of my table is:
Collection {#825 ▼
#items: array:19 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "rpx"
5 => "cahaya"
6 => "dse"
7 => "first"
8 => "indah"
9 => "jet"
10 => "jnt"
11 => "ncs"
12 => "pahala"
13 => "pandu"
14 => "sap"
15 => "sicepat"
16 => "slis"
17 => "star"
18 => "nss"
]
}
this is how i get that:
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier');
PS: as you see my table dd has 2 names less than my api dd that's
because those 2 names active status is 0 (deactivated)
Problem
what I try to do here is compare between my API courier names and my Database courier names (which are the same by the way) and then return only those couriers with status of 1 in my database (ignore the rest of courier names in my api)
how can i do this compare?
My full code is:
$cori = $rajaongkir->courier('all'); // get all couriers from api
$cori = array_keys($cori); // retrieve only their names
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier'); // get my DB couriers with active status
//comparing code...?
Simple, use array_intersect()
$result = array_intersect($selectedcourier, $cori);
I have a multidimensional array that I get from DB. Array has a number of views by each hour that is logged in the DB as a view, and it looks like this:
array:11 [▼
0 => array:2 [▼
"hour" => 0
"views" => 1
]
1 => array:2 [▼
"hour" => 1
"views" => 1
]
2 => array:2 [▼
"hour" => 4
"views" => 1
]
...and so on
]
I need to make a new array that will contain number of views for range of 2 hours. So for example from the array shown above I would like to get an array with, number of views for time between 0-2, that would 2, and for 2-4, would be 0 in this case, and so on.
You can do it in Mysql query:
select floor(hour/2) range, sum(views) sum
from thetable
group by range
You can just use a foreach to create a new array.
<?php
$your_array = [0 => [
"hour" => 0,
"views" => 4
],
1 => [
"hour" => 1,
"views" => 12
],
2 => [
"hour" => 4,
"views" => 1
],
3 => [
"hour" => 2,
"views" => 9
],
4 => [
"hour" => 21,
"views" => 19
]
];
foreach ($your_array as $value){
for($i=0;$i<=22;$i=$i+2){
$j=$i+2;
if($value['hour']>=$i && $value['hour']<$j){
isset($result[$i.'-'.$j])?$result[$i.'-'.$j]+=$value['views']:$result[$i.'-'.$j]=$value['views'];
}
}
}
print_r($result);
Try below code.
$arr = [0 => [
"hour" => 0,
"views" => 1
],
1 => [
"hour" => 1,
"views" => 1
],
2 => [
"hour" => 4,
"views" => 1
]];
foreach($arr as $row)
{
if($row['hour'] >= 0 && $row['hour'] <= 2)
{
$newArr['0-2'] = isset($newArr['0-2']) ? ($newArr['0-2'] + 1) : 1;
}
if($row['hour'] > 2 && $row['hour'] < 4)
{
$newArr['2-4'] = isset($newArr['2-4']) ? ($newArr['2-4'] + 1) : 1;
}
}
print_r($newArr);
Output
Array
(
[0-2] => 2
)