John Davidson

create new array with fixed date array in php

0 comments
Message:


Hi please help me to combine the two array
I want to find the combined array with the sum of count values and create new array as mentioned in the output



Is any possible to do like this array2 will be fixed size and date is
generated in array2 is from the current date to last 7 days



this is array 1


Array (
[0] => Array
(
[status] => failed
[date] => 2021-05-17
[count] => 1
)

[1] => Array
(
[status] => failed
[date] => 2021-05-12
[count] => 1
)

[2] => Array
(
[status] => failed
[date] => 2021-05-04
[count] => 2
)

[3] => Array
(
[status] => failed
[date] => 2021-05-03
[count] => 1
)

[4] => Array
(
[status] => success
[date] => 2021-05-17
[count] => 1
)

[5] => Array
(
[status] => no result publish
[date] => 2021-05-17
[count] => 9
)

[6] => Array
(
[status] => no result publish
[date] => 2021-05-11
[count] => 1
)
)

array 2


Array
(
[0] => 2021-05-18
[1] => 2021-05-17
[2] => 2021-05-16
[3] => 2021-05-15
[4] => 2021-05-14
[5] => 2021-05-13
[6] => 2021-05-12
[7] => 2021-05-11
)

output for the comabain


Array (
[0] => Array
(
[status] => failed
[2021-05-18]=>0
[2021-05-17] => 1
[2021-05-16]=>0
[2021-05-15]=>0
[2021-05-14]=>0
[2021-05-13]=>0
[2021-05-12]=>1
[2021-05-11]=>0
[geaterdays]=> 3 // sum of count greater than 7 days from current date
[grandTotal] => 5 // sum of all date counts
)

[1] => Array
(
[status] => success
[2021-05-18]=>0
[2021-05-17] => 1
[2021-05-16]=>0
[2021-05-15]=>0
[2021-05-14]=>0
[2021-05-13]=>0
[2021-05-12]=>0
[2021-05-11]=>0
[geaterdays]=> 0 // sum of count greater than 7 days from current date
[grandTotal] => 1 // sum of all date counts
)

[2] => Array
(
[status] => no result publish
[2021-05-18]=>0
[2021-05-17] => 9
[2021-05-16]=>0
[2021-05-15]=>0
[2021-05-14]=>0
[2021-05-13]=>0
[2021-05-12]=>0
[2021-05-11]=>1
[geaterdays]=> 0 // sum of count greater than 7 days from current date
[grandTotal] => 10 // sum of all date counts
)

)


My Answer:
You can achieve this by looping through both arrays and creating a new array with the desired structure. Here's a sample code to help you get started:

php
$array1 = [
['status' => 'failed', 'date' => '2021-05-17', 'count' => 1],
['status' => 'failed', 'date' => '2021-05-12', 'count' => 1],
['status' => 'failed', 'date' => '2021-05-04', 'count' => 2],
['status' => 'failed', 'date' => '2021-05-03', 'count' => 1],
['status' => 'success', 'date' => '2021-05-17', 'count' => 1],
['status' => 'no result publish', 'date' => '2021-05-17', 'count' => 9],
['status' => 'no result publish', 'date' => '2021-05-11', 'count' => 1]
];

$array2 = [
'2021-05-18',
'2021-05-17',
'2021-05-16',
'2021-05-15',
'2021-05-14',
'2021-05-13',
'2021-05-12',
'2021-05-11'
];

$output = [];

foreach ($array1 as $item) {
$status = $item['status'];
$output[$status]['status'] = $status;

foreach ($array2 as $date) {
$output[$status][$date] = 0;
}

$output[$status]['greaterdays'] = 0;
$output[$status]['grandTotal'] = 0;
}

foreach ($array1 as $item) {
$status = $item['status'];
$date = $item['date'];
$count = $item['count'];

$output[$status][$date] = $count;
$output[$status]['grandTotal'] += $count;
}

foreach ($output as $key => $value) {
$greaterdays = 0;
foreach ($array2 as $date) {
if (!array_key_exists($date, $value)) {
$greaterdays += $value[$date];
}
}

$output[$key]['greaterdays'] = $greaterdays;
}

print_r(array_values($output));


This code will create the desired output array based on the input arrays provided. Feel free to modify it according to your specific requirements.

Rate this post

5 of 5 based on 7655 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us