Laravel controller Merge 2 array to Comparision selling - php

I have 2 array to get the sold products from location at different date ranges then I need to make comparison between the two arrays
$results_day_report = DB::table('transaction_sell_lines as tsl')
->select(DB::raw('b.name as business_location_name , t.location_id as location_id , tsl.product_id as product_id , p.name as product_name , tsl.variation_id as variation_id , sum(tsl.quantity) AS quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS total'))
->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
->leftjoin('business_locations as b','b.id', '=', 't.location_id')
->leftjoin('products as p','p.id', '=', 'tsl.product_id')
->whereRaw("t.transaction_date LIKE '$reportdate%'")
->where('b.custom_field1', $location_type)
->where('t.type', '=' , 'sell')
->where('t.status', '=' , 'final')
->where('p.id', '!=' , 1)
->groupBy('tsl.variation_id')
->orderBy('tsl.variation_id', 'asc')
->orderBy('b.id', 'asc')
->get();
$results_day_report_year_before = DB::table('transaction_sell_lines as tsl')
->select(DB::raw('b.name as business_location_name , t.location_id as location_id , tsl.product_id as product_id , p.name as product_name , tsl.variation_id as variation_id , sum(tsl.quantity) AS quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS total'))
->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
->leftjoin('business_locations as b','b.id', '=', 't.location_id')
->leftjoin('products as p','p.id', '=', 'tsl.product_id')
->whereRaw("t.transaction_date LIKE '$reportdate_year_before%'")
->where('b.custom_field1', $location_type)
->where('t.type', '=' , 'sell')
->where('t.status', '=' , 'final')
->where('p.id', '!=' , 1)
->groupBy('tsl.variation_id')
->orderBy('tsl.variation_id', 'asc')
->orderBy('b.id', 'asc')
->get();
and Now I have results to be
$results_day_report =
Illuminate\Support\Collection {#4170 ▼
#items: array:23 [▼
0 => {#4172 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 526
+"product_name": "سيارات تصادم (مايكل)"
+"variation_id": 674
+"quantity": "15.0000"
+"total": "300.00000000"
}
1 => {#4178 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 528
+"product_name": "قطار اطفال (مايكل)"
+"variation_id": 676
+"quantity": "5.0000"
+"total": "50.00000000"
}
2 => {#4179 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 529
+"product_name": "ارنوب (مايكل)"
+"variation_id": 677
+"quantity": "3.0000"
+"total": "30.00000000"
}
3 => {#4173 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 594
+"product_name": "جم رجال يومى ع.د"
+"variation_id": 742
+"quantity": "3.0000"
+"total": "90.00000000"
}
4 => {#4171 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 595
+"product_name": "جم رجال يومى ق.م"
+"variation_id": 743
+"quantity": "1.0000"
+"total": "60.00000000"
}
5 => {#4177 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 597
+"product_name": "جم رجال شهرى ع.د"
+"variation_id": 745
+"quantity": "1.0000"
+"total": "200.00000000"
}
6 => {#4175 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 615
+"product_name": "رسم استخراج كارنيه اول مره"
+"variation_id": 763
+"quantity": "1.0000"
+"total": "30.00000000"
}
7 => {#4180 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 637
+"product_name": "جلسة مساج مشتركين"
+"variation_id": 785
+"quantity": "2.0000"
+"total": "400.00000000"
}
8 => {#4181 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 679
+"product_name": "جيم سيدات يومى ع د"
+"variation_id": 827
+"quantity": "2.0000"
+"total": "70.00000000"
}
9 => {#4182 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 681
+"product_name": "جيم سيدات يومى مدنى"
+"variation_id": 829
+"quantity": "1.0000"
+"total": "85.00000000"
}
10 => {#4183 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 683
+"product_name": "جيم سيدات شهرى ق م"
+"variation_id": 831
+"quantity": "1.0000"
+"total": "450.00000000"
}
11 => {#4184 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 705
+"product_name": "حمام سباحة مغطى(م)"
+"variation_id": 853
+"quantity": "1.0000"
+"total": "75.00000000"
}
12 => {#4185 ▼
+"business_location_name": "Subscriptions"
+"location_id": 20
+"product_id": 773
+"product_name": "العاب ثابته"
+"variation_id": 921
+"quantity": "3.0000"
+"total": "30.00000000"
}
13 => {#4186 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 6140
+"product_name": "الترامبولين م"
+"variation_id": 6291
+"quantity": "7.0000"
+"total": "70.00000000"
}
14 => {#4187 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 6568
+"product_name": "كاروسيل (مايكل)"
+"variation_id": 6719
+"quantity": "5.0000"
+"total": "50.00000000"
}
15 => {#4188 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 7781
+"product_name": "مصارعه سومو"
+"variation_id": 7932
+"quantity": "10.0000"
+"total": "217.39100000"
}
16 => {#4189 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 7782
+"product_name": "تيلي جيمز"
+"variation_id": 7933
+"quantity": "10.0000"
+"total": "217.39100000"
}
17 => {#4190 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 7783
+"product_name": "فولي بيج بول"
+"variation_id": 7934
+"quantity": "10.0000"
+"total": "347.82600000"
}
18 => {#4191 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 7784
+"product_name": "سوكر شوز"
+"variation_id": 7935
+"quantity": "10.0000"
+"total": "347.82600000"
}
19 => {#4192 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 7785
+"product_name": "الساعة"
+"variation_id": 7936
+"quantity": "10.0000"
+"total": "434.78300000"
}
20 => {#4193 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 9091
+"product_name": "اكستريم جيم سيدات شهري مدني"
+"variation_id": 9251
+"quantity": "1.0000"
+"total": "800.00000000"
}
21 => {#4194 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 9341
+"product_name": "ارشيرلي فايت"
+"variation_id": 9501
+"quantity": "10.0000"
+"total": "434.78300000"
}
22 => {#4195 ▼
+"business_location_name": "Red Zone"
+"location_id": 44
+"product_id": 9342
+"product_name": "كومبو (2)"
+"variation_id": 9502
+"quantity": "10.0000"
+"total": "2000.00000000"
}
]
}
and result of $results_day_report_year_before to be
Illuminate\Support\Collection {#4174 ▼
#items: array:15 [▼
0 => {#4196 ▼
+"business_location_name": "Video Game"
+"location_id": 18
+"product_id": 513
+"product_name": "موتوسيكل"
+"variation_id": 661
+"quantity": "1.0000"
+"total": "15.00000000"
}
1 => {#4202 ▼
+"business_location_name": "Video Game"
+"location_id": 18
+"product_id": 517
+"product_name": "هزازات"
+"variation_id": 665
+"quantity": "1.0000"
+"total": "10.00000000"
}
2 => {#4203 ▼
+"business_location_name": "Video Game"
+"location_id": 18
+"product_id": 520
+"product_name": "هدايا كبيرة"
+"variation_id": 668
+"quantity": "1.0000"
+"total": "20.00000000"
}
3 => {#4197 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 526
+"product_name": "سيارات تصادم (مايكل)"
+"variation_id": 674
+"quantity": "24.0000"
+"total": "480.00000000"
}
4 => {#4176 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 527
+"product_name": "سامبا (مايكل)"
+"variation_id": 675
+"quantity": "2.0000"
+"total": "40.00000000"
}
5 => {#4201 ▼
+"business_location_name": "Gaming Area"
+"location_id": 19
+"product_id": 528
+"product_name": "قطار اطفال (مايكل)"
+"variation_id": 676
+"quantity": "4.0000"
+"total": "40.00000000"
}
6 => {#4199 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 594
+"product_name": "جم رجال يومى ع.د"
+"variation_id": 742
+"quantity": "1.0000"
+"total": "30.00000000"
}
7 => {#4204 ▼
+"business_location_name": "Lavandula Gym"
+"location_id": 17
+"product_id": 598
+"product_name": "جم رجال شهرى ق.م"
+"variation_id": 746
+"quantity": "1.0000"
+"total": "350.00000000"
}
8 => {#4205 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 615
+"product_name": "رسم استخراج كارنيه اول مره"
+"variation_id": 763
+"quantity": "5.0000"
+"total": "50.00000000"
}
9 => {#4206 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 688
+"product_name": "جم رجال يومى ع.د"
+"variation_id": 836
+"quantity": "2.0000"
+"total": "70.00000000"
}
10 => {#4207 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 690
+"product_name": "جم رجال يومى مدنى"
+"variation_id": 838
+"quantity": "1.0000"
+"total": "85.00000000"
}
11 => {#4208 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 691
+"product_name": "جم رجال شهرى ع.د"
+"variation_id": 839
+"quantity": "3.0000"
+"total": "750.00000000"
}
12 => {#4209 ▼
+"business_location_name": "Tiba Rose Gym"
+"location_id": 16
+"product_id": 692
+"product_name": "جم رجال شهرى ق.م"
+"variation_id": 840
+"quantity": "1.0000"
+"total": "450.00000000"
}
13 => {#4210 ▼
+"business_location_name": "Subscriptions"
+"location_id": 20
+"product_id": 763
+"product_name": "حجزملعب س/ط س ن"
+"variation_id": 911
+"quantity": "4.0000"
+"total": "560.00000000"
}
14 => {#4211 ▼
+"business_location_name": "Subscriptions"
+"location_id": 20
+"product_id": 773
+"product_name": "العاب ثابته"
+"variation_id": 921
+"quantity": "2.0000"
+"total": "20.00000000"
}
]
}
then I make this foreach loop to generate one array with the comparison
foreach($results_day_report as $result_day_report)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report->business_location_name,
'location_id' => $result_day_report->location_id,
'product_id' => $result_day_report->product_id,
'product_name' => $result_day_report->product_name,
'variation_id' => $result_day_report->variation_id,
'quantity_report_date_same_year' => $result_day_report->quantity,
'total_report_date_same_year' => $result_day_report->total,
'quantity_report_date_year_before' => 0,
'total_report_date_year_before' => 0,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_a' => '1',
]);
}
foreach($results_day_report_year_before as $result_day_report_year_before)
{
$collection = collect($queries);
if(!$collection->contains('variation_id', $result_day_report_year_before->variation_id))
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '1',
]);
}
else
{
$data = $collection->map(function ($item, $key) use ($result_day_report_year_before) {
if($item->location_id == $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
$item->quantity_report_date_year_before = $result_day_report_year_before->quantity ;
$item->total_report_date_year_before = $result_day_report_year_before->total ;
$item->step_b = 2 ;
}
elseif($item->location_id != $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '3',
]);
}
return $item;
});
}
}
Now the result of final array ($queries) will be
This result isn't correct, the correct answer must be array with 34 object not 33 object
Error at step of array push which contain 'step_b' => '3' , it return null not correct object
What's the problem ?

I find the problem on my code
the correct code is
$collection->map(function ($item, $key) use ($result_day_report_year_before ,&$queries) {
if($item->location_id == $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
$item->quantity_report_date_year_before = $result_day_report_year_before->quantity ;
$item->total_report_date_year_before = $result_day_report_year_before->total ;
$item->step_b = 2 ;
}
elseif($item->location_id != $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '3',
]);
}
});
The error was in using the array in the map function
Thanks

Related

range(strtotime("00:00"), strtotime("23:59"), 30*60) does not work properly after upgrade laravel to 7. Is it php problem?

After upgrade Laravel 7 from 6 I have got an error.
Array used to have total 48 items but now 50 items.
Is it php error? or laravel error?
I tested on laravel 5 it gives total 48 items.
$times = [];
$range= range(strtotime("00:00"), strtotime("23:59"), 30*60);
foreach($range as $time){
$times[] = date("H:i",$time);
}
return $times;
array:50 [▼
0 => "00:00"
1 => "00:30"
2 => "01:00"
3 => "01:30"
4 => "02:00"
5 => "02:30"
6 => "02:00"
7 => "02:30"
8 => "03:00"
9 => "03:30"
10 => "04:00"
11 => "04:30"
12 => "05:00"
13 => "05:30"
14 => "06:00"
15 => "06:30"
16 => "07:00"
17 => "07:30"
18 => "08:00"
19 => "08:30"
20 => "09:00"
21 => "09:30"
22 => "10:00"
23 => "10:30"
24 => "11:00"
25 => "11:30"
26 => "12:00"
27 => "12:30"
28 => "13:00"
29 => "13:30"
30 => "14:00"
31 => "14:30"
32 => "15:00"
33 => "15:30"
34 => "16:00"
35 => "16:30"
36 => "17:00"
37 => "17:30"
38 => "18:00"
39 => "18:30"
40 => "19:00"
41 => "19:30"
42 => "20:00"
43 => "20:30"
44 => "21:00"
45 => "21:30"
46 => "22:00"
47 => "22:30"
48 => "23:00"
49 => "23:30"
]
6 => "02:00" and 7 => "02:30" are duplicated.
Any advice please?
Thanks
Found the reason why. It is because of day light saving. #JoffreySchmitz commented. Thanks.

Laravel: Count integer value based on week of the month

I'm trying to count integer value from JSON: https://pomber.github.io/covid19/timeseries.json and got what I expected.
{
"date": [
"22 Jan",
"23 Jan",
"24 Jan",
"25 Jan",
....
],
"total_confirmed": [
555,
653,
941,
1434,
....
],
"total_deaths": [
17,
18,
26,
42,
....
],
"total_recovered": [
28,
30,
36,
39,
....
],
"max_value_of_total_confirmed": 272166
}
Here's my controller:
$client = new Client();$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
$confirmed_array = array();
$deaths_array = array();
$recovered_array = array();
if ( ! empty( $posts_dates ) ) {
foreach ( $posts_dates as $country => $data ) {
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($confirmed_array[$day]) ) {
$confirmed_array[$day] = 0;
}
$confirmed_array[$day] += $dateData['confirmed'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($deaths_array[$day]) ) {
$deaths_array[$day] = 0;
}
$deaths_array[$day] += $dateData['deaths'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($recovered_array[$day]) ) {
$recovered_array[$day] = 0;
}
$recovered_array[$day] += $dateData['recovered'];
}
}
}
$output = [
"date" => array_keys($confirmed_array),
"total_confirmed" => array_values($confirmed_array),
"total_deaths" => array_values($deaths_array),
"total_recovered" => array_values($recovered_array),
"max_value_of_total_confirmed" => max($confirmed_array)
];
return $output;
What I'm trying to get is grouping those data not by date, but by week of the month (W3 Jan, W4 Jan, W1 Feb, W2 Feb, etc). Any help would be appreciated :)
Thank you.
I would recommend using collection. You can easily manage using the methods provided by Laravel.
Group By Day
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->groupBy('date')
->map(function ($item, $key) {
$item = collect($item);
$date = \Carbon\Carbon::parse($key);
$key = 'W' . $date->weekOfMonth . $date->format(' M');
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:59 [
0 => "W4 Jan"
1 => "W4 Jan"
2 => "W4 Jan"
3 => "W4 Jan"
4 => "W4 Jan"
5 => "W4 Jan"
6 => "W4 Jan"
7 => "W5 Jan"
8 => "W5 Jan"
9 => "W5 Jan"
10 => "W1 Feb"
11 => "W1 Feb"
12 => "W1 Feb"
13 => "W1 Feb"
14 => "W1 Feb"
15 => "W1 Feb"
16 => "W1 Feb"
17 => "W2 Feb"
18 => "W2 Feb"
19 => "W2 Feb"
20 => "W2 Feb"
21 => "W2 Feb"
22 => "W2 Feb"
23 => "W2 Feb"
24 => "W3 Feb"
25 => "W3 Feb"
26 => "W3 Feb"
27 => "W3 Feb"
28 => "W3 Feb"
29 => "W3 Feb"
30 => "W3 Feb"
31 => "W4 Feb"
32 => "W4 Feb"
33 => "W4 Feb"
34 => "W4 Feb"
35 => "W4 Feb"
36 => "W4 Feb"
37 => "W4 Feb"
38 => "W5 Feb"
39 => "W1 Mar"
40 => "W1 Mar"
41 => "W1 Mar"
42 => "W1 Mar"
43 => "W1 Mar"
44 => "W1 Mar"
45 => "W1 Mar"
46 => "W2 Mar"
47 => "W2 Mar"
48 => "W2 Mar"
49 => "W2 Mar"
50 => "W2 Mar"
51 => "W2 Mar"
52 => "W2 Mar"
53 => "W3 Mar"
54 => "W3 Mar"
55 => "W3 Mar"
56 => "W3 Mar"
57 => "W3 Mar"
58 => "W3 Mar"
]
"total_confirmed" => array:59 [
0 => 555
1 => 653
2 => 941
3 => 1434
4 => 2118
5 => 2927
6 => 5578
7 => 6166
8 => 8234
9 => 9927
10 => 12038
11 => 16787
12 => 19881
13 => 23892
14 => 27635
15 => 30817
16 => 34391
17 => 37120
18 => 40150
19 => 42762
20 => 44802
21 => 45221
22 => 60368
23 => 66885
24 => 69030
25 => 71224
26 => 73258
27 => 75136
28 => 75639
29 => 76197
30 => 76823
31 => 78579
32 => 78965
33 => 79568
34 => 80413
35 => 81395
36 => 82754
37 => 84120
38 => 86011
39 => 88369
40 => 90306
41 => 92840
42 => 95120
43 => 97882
44 => 101784
45 => 105821
46 => 109795
47 => 113561
48 => 118592
49 => 125865
50 => 128343
51 => 145193
52 => 156094
53 => 167446
54 => 181527
55 => 197142
56 => 214910
57 => 242708
58 => 272166
]
"total_deaths" => array:59 [
0 => 17
1 => 18
2 => 26
3 => 42
4 => 56
5 => 82
6 => 131
7 => 133
8 => 171
9 => 213
10 => 259
11 => 362
12 => 426
13 => 492
14 => 564
15 => 634
16 => 719
17 => 806
18 => 906
19 => 1013
20 => 1113
21 => 1118
22 => 1371
23 => 1523
24 => 1666
25 => 1770
26 => 1868
27 => 2007
28 => 2122
29 => 2247
30 => 2251
31 => 2458
32 => 2469
33 => 2629
34 => 2708
35 => 2770
36 => 2814
37 => 2872
38 => 2941
39 => 2996
40 => 3085
41 => 3160
42 => 3254
43 => 3348
44 => 3460
45 => 3558
46 => 3802
47 => 3988
48 => 4262
49 => 4615
50 => 4720
51 => 5404
52 => 5819
53 => 6440
54 => 7126
55 => 7905
56 => 8733
57 => 9867
58 => 11299
]
"total_recovered" => array:59 [
0 => 28
1 => 30
2 => 36
3 => 39
4 => 52
5 => 61
6 => 107
7 => 126
8 => 143
9 => 222
10 => 284
11 => 472
12 => 623
13 => 852
14 => 1124
15 => 1487
16 => 2011
17 => 2616
18 => 3244
19 => 3946
20 => 4683
21 => 5150
22 => 6295
23 => 8058
24 => 9395
25 => 10865
26 => 12583
27 => 14352
28 => 16121
29 => 18177
30 => 18890
31 => 22886
32 => 23394
33 => 25227
34 => 27905
35 => 30384
36 => 33277
37 => 36711
38 => 39782
39 => 42716
40 => 45602
41 => 48228
42 => 51170
43 => 53796
44 => 55865
45 => 58358
46 => 60694
47 => 62494
48 => 64404
49 => 67003
50 => 68324
51 => 70251
52 => 72624
53 => 76034
54 => 78088
55 => 80840
56 => 83207
57 => 84854
58 => 87256
]
"max_value_of_total_confirmed" => 272166
]
Group By Week
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->map(function ($item) {
$date = \Carbon\Carbon::parse($item['date']);
$item['week'] = 'W' . $date->weekOfMonth . $date->format(' M');
return $item;
})
->groupBy('week')
->map(function ($item, $key) {
$item = collect($item);
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:10 [
0 => "W4 Jan"
1 => "W5 Jan"
2 => "W1 Feb"
3 => "W2 Feb"
4 => "W3 Feb"
5 => "W4 Feb"
6 => "W5 Feb"
7 => "W1 Mar"
8 => "W2 Mar"
9 => "W3 Mar"
]
"total_confirmed" => array:10 [
0 => 14206
1 => 24327
2 => 165441
3 => 337308
4 => 517307
5 => 565794
6 => 86011
7 => 672122
8 => 897443
9 => 1275899
]
"total_deaths" => array:10 [
0 => 372
1 => 517
2 => 3456
3 => 7850
4 => 13931
5 => 18720
6 => 2941
7 => 22861
8 => 32610
9 => 51370
]
"total_recovered" => array:10 [
0 => 353
1 => 491
2 => 6853
3 => 33992
4 => 100383
5 => 199784
6 => 39782
7 => 355735
8 => 465794
9 => 490279
]
"max_value_of_total_confirmed" => 1275899
]

How do I convert an array based on keys in PHP?

I am trying to divide an array based on values within the array, and to sort it based on keys that the array also has. My array currently looks like this:
array:6 [▼
"player_ids" => array:8 [▼
0 => "103"
1 => "221"
2 => "283"
3 => "321"
4 => "333"
5 => "406"
6 => "425"
7 => "428"
]
"game_id" => array:8 [▼
103 => "33058041"
221 => "33058041"
283 => "33058041"
321 => "33058041"
333 => "33058041"
406 => "33058041"
425 => "33058041"
428 => "33058041"
]
"goals" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"assists" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"yellows" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"red" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
]
with arrays with each of the players embedded in a particular field. How would I manipulate this array to make it so that player_ids would become the first key, like so:
"103" => array:6 [▼
player_ids => "103"
game_id => "33058041"
goals => "0"
assists => "0"
yellows => "0"
red => "0"
]
for each of the player id's listed?
you can loop through the player_ids key in your big array and create a new array with the structure you want. Something like this.
$newArray = array();
foreach ($array['player_ids'] as $p){
$newArray[$p] = array (
'player_ids' => $p,
'game_id' => $array['game_id'][$p],
'goals' => $array['goals'][$p],
'assists' => $array['assists'][$p],
'yellows' => $array['yellows'][$p],
'red' => $array['red'][$p]
)
}
EDIT: As I'm posting this, it's really just a slightly different way to do what #Long Kim already posted, although his is a bit more elegant for this scenario. I'd go with that... The method below is better for a larger array though.
You don't provide a name for your 'master array', so I'm calling it $masterArray here. $newArray is what I believe you're looking for.
$newArray = array();
foreach($masterArray['player_ids'] as $playerid){
foreach($masterArray as $key => $value){
if($key=="player_ids"){
$newArray[$playerid][$key] = $playerid;
}
else{
$newArray[$playerid][$key] = $value[$playerid];
}
}
}

Parsing conditions in a custom DSL using a tokenizer

I am trying to parse a "rules string" provided over an API in PHP.
Given that I have an array of items such as
$items = [41, 42, 51, 54, 65, 12];
I wish to evaluate this expression
({41} AND {51}) OR ({31} AND {42})
In this instance it should return true, since the $items array has both the 41 and 51 items.
I started with creating a tokenizer using the https://github.com/nette/tokenizer library and currently have it parsing the string into an array of tokens.
array:25 [
0 => array:3 [
0 => "("
1 => 0
2 => 1
]
1 => array:3 [
0 => "{"
1 => 1
2 => 2
]
2 => array:3 [
0 => "41"
1 => 2
2 => 3
]
3 => array:3 [
0 => "}"
1 => 4
2 => 4
]
4 => array:3 [
0 => " "
1 => 5
2 => 8
]
5 => array:3 [
0 => "AND"
1 => 6
2 => 5
]
6 => array:3 [
0 => " "
1 => 9
2 => 8
]
7 => array:3 [
0 => "{"
1 => 10
2 => 2
]
8 => array:3 [
0 => "51"
1 => 11
2 => 3
]
9 => array:3 [
0 => "}"
1 => 13
2 => 4
]
10 => array:3 [
0 => ")"
1 => 14
2 => 7
]
11 => array:3 [
0 => " "
1 => 15
2 => 8
]
12 => array:3 [
0 => "OR"
1 => 16
2 => 6
]
13 => array:3 [
0 => " "
1 => 18
2 => 8
]
14 => array:3 [
0 => "("
1 => 19
2 => 1
]
15 => array:3 [
0 => "{"
1 => 20
2 => 2
]
16 => array:3 [
0 => "31"
1 => 21
2 => 3
]
17 => array:3 [
0 => "}"
1 => 23
2 => 4
]
18 => array:3 [
0 => " "
1 => 24
2 => 8
]
19 => array:3 [
0 => "AND"
1 => 25
2 => 5
]
20 => array:3 [
0 => " "
1 => 28
2 => 8
]
21 => array:3 [
0 => "{"
1 => 29
2 => 2
]
22 => array:3 [
0 => "42"
1 => 30
2 => 3
]
23 => array:3 [
0 => "}"
1 => 32
2 => 4
]
24 => array:3 [
0 => ")"
1 => 33
2 => 7
]
]
However this is where my talent runs out and I am not sure how to process the tokens so that I can run the condition against my $items array.
It could be that I am approaching this completely wrong.

How to get value from all index of collection in laravel Illuminate?

I have to query data from DB to using Illuminate in laravel then I want to get all the attribute value from all index of this array.
array:36 [▼
0 => RepaymentSchedule {#553 ▶}
1 => RepaymentSchedule {#554 ▶}
2 => RepaymentSchedule {#555 ▶}
3 => RepaymentSchedule {#556 ▶}
4 => RepaymentSchedule {#557 ▶}
5 => RepaymentSchedule {#558 ▶}
6 => RepaymentSchedule {#559 ▶}
7 => RepaymentSchedule {#560 ▶}
8 => RepaymentSchedule {#561 ▶}
9 => RepaymentSchedule {#562 ▶}
10 => RepaymentSchedule {#563 ▶}
11 => RepaymentSchedule {#564 ▶}
12 => RepaymentSchedule {#565 ▶}
13 => RepaymentSchedule {#566 ▶}
14 => RepaymentSchedule {#567 ▶}
15 => RepaymentSchedule {#568 ▶}
16 => RepaymentSchedule {#569 ▶}
17 => RepaymentSchedule {#570 ▶}
18 => RepaymentSchedule {#571 ▶}
19 => RepaymentSchedule {#572 ▶}
20 => RepaymentSchedule {#573 ▶}
21 => RepaymentSchedule {#574 ▶}
22 => RepaymentSchedule {#575 ▶}
23 => RepaymentSchedule {#576 ▶}
24 => RepaymentSchedule {#577 ▶}
25 => RepaymentSchedule {#578 ▶}
26 => RepaymentSchedule {#579 ▶}
27 => RepaymentSchedule {#580 ▶}
28 => RepaymentSchedule {#581 ▶}
29 => RepaymentSchedule {#582 ▶}
30 => RepaymentSchedule {#583 ▶}
31 => RepaymentSchedule {#584 ▶}
32 => RepaymentSchedule {#585 ▶}
33 => RepaymentSchedule {#586 ▶}
34 => RepaymentSchedule {#587 ▶}
35 => RepaymentSchedule {#588 ▶}
]
I have try to used bellow
$collapsed = $loan->schedule->all();
dd($collapsed);
$collection = $collapsed->each(function ($item, $key) {
dd($key);
});
In laravel 5.1 and above you can use the pluck method of the Collection object:
$plucked = $collapsed->pluck('id');
dd($plucked->all());
Docs: https://laravel.com/docs/5.2/collections#method-pluck
But since you are using L5.0 this isnt possible, I suggest you solve this by the PHP5 method array_column function instead:
$ids = array_column($collapsed->toArray(), 'id');
dd($ids);
Edit:
If you want to retrieve more than one attribute you can use the ->lists() method:
$values = $loan->schedule->lists('id', 'interest', 'principal');
dd($values);

Categories