how to WhereIn a whereday of array in laravel - php

array:23 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 8
6 => 9
7 => 10
8 => 11
9 => 12
10 => 15
11 => 16
12 => 17
13 => 18
14 => 19
15 => 22
16 => 23
17 => 24
18 => 25
19 => 26
20 => 29
21 => 30
22 => 31
]
this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe
->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data

There is no whereDayIn() or similar method, but you can do this:
->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
foreach ($workdays as $day) {
$q->whereDay('Clock_Day', '=', $day, 'or');
}
})

If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:
->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)
You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.

Related

laravel sorting arrays last 5 unique and reverse

I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856
You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online
In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();
try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);

PHP Trader macd returns false

I want to use trader_macd but it returns always false.
I am using the default parameters:
$data = [
0 => "0.06945900",
1 => "0.06945200",
2 => "0.06948100",
3 => "0.06944100",
4 => "0.06939800",
5 => "0.06941800",
6 => "0.06942300",
7 => "0.06940000",
8 => "0.06937700",
9 => "0.06937200",
10 => "0.06940000",
11 => "0.06939800",
12 => "0.06941100",
13 => "0.06944500",
14 => "0.06940100",
15 => "0.06942600",
16 => "0.06941500",
17 => "0.06941400",
18 => "0.06939900",
19 => "0.06941400",
20 => "0.06940700",
21 => "0.06938100",
22 => "0.06940400",
23 => "0.06937400",
24 => "0.06937000",
25 => "0.06939700"]
$result = trader_macd($data, 12, 26, 9)
When I set the last parameter ($signalPeriod) then get an array with 0 values:
0 => array:1 [▼
24 => -0.0
]
1 => array:1 [▼
24 => -0.0
]
2 => array:1 [▼
24 => -0.0
]
When I am using other methods like trader_ema with same $data it works fine.
I also set trader.real_precision to 8.
ini_set('trader.real_precision', '8');
What I am doing wrong?
My Systems uses php 7.2.7 with trader 0.5.0.
You don't have enough data to calculate the signal line you chose (9 day EMA of MACD line). Add eight more entries to your data array, and you'll get a result. Or lower the signal line period

Laravel compare database and api

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

How to Create complex Array Structure in PHP

I have to make this kind of structure in array;
We have three ( 3 ) variables which creates this structure:
$numberOfParticipants = 38; // 38 is example
$numberOfParticipantsPerHeat = 8 // 8 is example
$numberOfHeats = 5; // 5 is example
Based on this variables I have this table:
The problem is that, I can't place the ' - ' or null after 31 OR 38. The task is that i have to make the arrays of array "almost equal" like the photo and must depend on the variables above. By the way, after I create the correct list I will slice the array to 5 or 6 or whatever parts I need this is not the problem, the problem is that I have to parse the list like this first. This is what I tried so far:
$calc1 = (int)round($numberOfParticipants * $numberOfParticipantsPerHeat, -1); //First round the numberOfParticipants to closest integer by 10
$readyArr = [];
for ($i = 1; $i <= $calc1; $i++) {
if ($i <= $numberOfParticipants) {
$readyArr[$i] = $i;
} else {
$readyArr[$i] = null;
}
}
The problem with this snippet is that it places the null at the end of the list not after 31, or based on the var.
This is the result I have:
array:40 [▼
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
7 => 7
8 => 8
9 => 9
10 => 10
11 => 11
12 => 12
13 => 13
14 => 14
15 => 15
16 => 16
17 => 17
18 => 18
19 => 19
20 => 20
21 => 21
22 => 22
23 => 23
24 => 24
25 => 25
26 => 26
27 => 27
28 => 28
29 => 29
30 => 30
31 => 31
32 => 32
33 => 33
34 => 34
35 => 35
36 => 36
37 => 37
38 => 38
39 => null
40 => null
]
The Array after partition I want should be:
array(
0 => array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8,),
1 => array(0 => 9, 1 => 10, 2 => 11, 3 => 12, 4 => 13, 5 => 14, 6 => 15, 7 => 16,),
2 => array(0 => 17, 1 => 18, 2 => 19, 3 => 20, 4 => 21, 5 => 22, 6 => 23, 7 => 24,),
3 => array(0 => 25, 1 => 26, 2 => 27, 3 => 28, 4 => 29, 5 => 30, 6 => 31, 7 => null,),
4 => array(0 => 32, 1 => 33, 2 => 34, 3 => 35, 4 => 36, 5 => 37, 6 => 38, 7 => null,),
);
Every help, every clue will be highly appreciated.
There are two things you need to know about the target structure:
How many players are in the first (which will always be the largest, if only by one) set.
$playersPerHeat = ceil($numberOfParticipants / $numberOfHeats);
// note this replaces your hard-coded $numberOfParticipantsPerHeat
You also need to know how many heats actually have that many, that is how many heats are actually full.
$fullHeats = $numberOfParticipants % $numberOfHeats ?: $numberOfHeats;
// The ?: bit means that if we get zero (ie. all equal heats), then we
// count all the heats instead, since they're all full.
Now it's easy!
$players = range(1,$numberOfParticipants);
$heats = array_merge(
array_chunk(
array_slice($players, 0, $fullHeats * $playersPerHeat),
$playersPerHeat
),
array_chunk(
array_slice($players, $fullHeats * $playersPerHeat),
$playersPerHeat - 1
)
);
That's it! Demo

store monthly date into array from the selected date

Assume that selected date from Canlender is 02/09/2011. To store weekly date into array from 20/09/2011 is
for($i=0; $i<7; $i++)
{
$WeeklyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
My question is how to store monthly date into array from the selected date.
Many thanks
---Update----------
The final result of monthlyDate should look like the following:
$monthlyDate= array{2011-08-03, 2011-08-04, 2011-08-05, 2011-08-06, 2011-08-07 ....2011-08-31, 2011-09-01, 2011-09-02}
First, calculate the number of days in a month using cal_days_in_month and then proceed as you are doing with weeks eg:
$days = cal_days_in_month(CAL_GREGORIAN, 9, 2011);
for($i = 0; $i <= $days; $i++)
{
$MonthlyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
Notice that CAL_GREGORIAN is a built-in constant.
Working Example
Whenever programs are incrementing a date using 86400 there is a risk of unexpected output because of DST.
By using strtotime() with a unit larger than hours (like days, weeks, months, etc.) preventing any DST hiccups. Note: a DateTime object approach can be used but for this case, it is unnecessary overhead.
The following is an adjusted form of a one-liner date range function I developed.
Here is the online demo for this case.
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
$date='2011-09-02';
$monthlyDate=getDatesFromRange(date("Y-m-d",strtotime("$date -1 month +1 day")),$date);
var_export($monthlyDate);
output as desired/expected:
array (
0 => '2011-08-03',
1 => '2011-08-04',
2 => '2011-08-05',
3 => '2011-08-06',
4 => '2011-08-07',
5 => '2011-08-08',
6 => '2011-08-09',
7 => '2011-08-10',
8 => '2011-08-11',
9 => '2011-08-12',
10 => '2011-08-13',
11 => '2011-08-14',
12 => '2011-08-15',
13 => '2011-08-16',
14 => '2011-08-17',
15 => '2011-08-18',
16 => '2011-08-19',
17 => '2011-08-20',
18 => '2011-08-21',
19 => '2011-08-22',
20 => '2011-08-23',
21 => '2011-08-24',
22 => '2011-08-25',
23 => '2011-08-26',
24 => '2011-08-27',
25 => '2011-08-28',
26 => '2011-08-29',
27 => '2011-08-30',
28 => '2011-08-31',
29 => '2011-09-01',
30 => '2011-09-02',
)

Categories