create new array with fixed date array in php - php

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

Related

PHP: Manipulating and removing duplicate element conditionally on Multidimensional array

I have following multidimensional array. I am not getting suitable title for my question. You are free to edit and change.
Array(
[2018-05-01] => Array
(
[0] => Array
(
[Zone] => 0
[Time] => 13:06
)
[1] => Array
(
[Zone] => 0
[Time] => 13:08
)
[2] => Array
(
[Zone] => 2
[Time] => 13:10
)
[3] => Array
(
[Zone] => 2
[Time] => 13:14
)
[4] => Array
(
[Zone] => 0
[Time] => 13:16
)
[5] => Array
(
[Zone] => 0
[Time] => 13:17
)
[6] => Array
(
[Zone] => 2
[Time] => 13:21
)
[7] => Array
(
[Zone] => 2
[Time] => 13:25
)
[8] => Array
(
[Zone] => 0
[Time] => 13:28
)
[9] => Array
(
[Zone] => 0
[Time] => 13:32
)
)
[2018-05-04] => Array
(
[0] => Array
(
[Zone] => 1
[Time] => 13:26
)
[1] => Array
(
[Zone] => 1
[Time] => 13:28
)
[2] => Array
(
[Zone] => 1
[Time] => 13:30
)
[3] => Array
(
[Zone] => 0
[Time] => 13:34
)
[4] => Array
(
[Zone] => 0
[Time] => 13:36
)
[5] => Array
(
[Zone] => 1
[Time] => 13:37
)
[6] => Array
(
[Zone] => 1
[Time] => 13:41
)
)
)
I have to manipulate above array in such a way that, Same Zone should not come simultaneously. And this depend upon the conditions -
Condition 1
If Sub array has only Zone => 0 and Zone => 2. For example array with key 2018-05-01 has only 0 and 2 as Zones.
We will only fetch First Zone => 0, last Zone => 2 , first Zone => 0 and again last Zone => 2.
Note here, if there is no Zone => 2 after Zone => 0, then in this case we fetch last Zone => 0 instead of first Zone => 0.
Please see my output for clearification.
Condition 2
If Sub array has Zone => 1. For example array with key 2018-05-04. We will fetch only First occurrence of each.
Here is the desired output
Array(
[2018-05-01] => Array
(
[0] => Array
(
[Zone] => 0
[Time] => 13:06
)
[3] => Array
(
[Zone] => 2
[Time] => 13:14
)
[4] => Array
(
[Zone] => 0
[Time] => 13:16
)
[7] => Array
(
[Zone] => 2
[Time] => 13:25
)
[9] => Array
(
[Zone] => 0
[Time] => 13:32
)
)
[2018-05-04] => Array
(
[0] => Array
(
[Zone] => 1
[Time] => 13:26
)
[3] => Array
(
[Zone] => 0
[Time] => 13:34
)
[5] => Array
(
[Zone] => 1
[Time] => 13:37
)
)
)
What I have tried is -
What I have been thinking of doing is using array_column to check if the sub array has "1" as Zone value. If yes then we will implement condition 1 otherwise Condition 2. Then loop through each subarray and comparing current iteration with previous one. But I am not getting exact view, and I m doubtful if this will be an elegant solution.

Joining two multidimensional arrays

I have two arrays each coming from different DB queries, I can't join them in DB so I have to join them using php.
The first:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[status] => 1
)
)
And I wanna join it with this array on pos_id and id
Array
(
[0] => Array
(
[id] => 1
[name] => Front
)
[1] => Array
(
[id] => 2
[name] => Back
)
)
so that, the result is:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[name] => Front
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
)
The code I have uses an inner loop, matches the value and pushes into array:
foreach($events as &$event) {
foreach($positions as $position) {
if($player[pos_id] == $position[id]){
array_push($event,"name",$position[name]);
}
}
}

sum of array based on key(Group by user)

enter image description here
This is my first array which i get from mysql
Array
([0] => Array
(
[created_by] => 4
[count] => 1
[total] => 300
)
[1] => Array
(
[created_by] => 4
[count] => 1
[total] => 450
)
[2] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
)
I need a output as
[0] => Array
(
[created_by] => 4
[count] => 2
[total] => 750
)
[1] => Array
(
[created_by] => 3
[count] => 1
[total] => 500
)
Sum of array total based on created_by
Need to group by created_by count.

Count subarray elements and sum deep subarray elements

I'm working with an API that returns a JSON object which I then decode into an array using
I need to get a count of all the [data] items in the array, and then generate a total of all the [Total] values contained in each
Here's my array:-
Array
(
[errorCode] => 0
[result] => OK
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 128.97
[Customers::Company] => A B C Lock & Key
[Status] => Paid
[Date Payment] => 11/30/2017
)
[portalData] => Array
(
)
[recordId] => 1
[modId] => 4
)
[1] => Array
(
[fieldData] => Array
(
[Due Date] => 12/01/2017
[Date] => 12/01/2017
[Total] => 256
[Customers::Company] => Kim Peacock Beringhause
[Status] => Paid
[Date Payment] => 12/01/2017
)
[portalData] => Array
(
)
[recordId] => 2
[modId] => 3
)
[2] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 1880
[Customers::Company] => Norton, Robert L Esq
[Status] => Unpaid Overdue
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 3
[modId] => 0
)
[3] => Array
(
[fieldData] => Array
(
[Due Date] => 12/22/2017
[Date] => 12/22/2017
[Total] => 1278
[Customers::Company] => Shapiro, Mark R Esq
[Status] => Unpaid
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 10
[modId] => 1
)
)
)
I've tried:
count($array)
which returns 3 which I can see are the count of the 3 items in that parent array (errorCode, result and data), but I can't work out how to focus on the data only.
The count value I'm after is 4 and the total value is 3542.97.
For count you need to do:
count($array['data']);
And for getting sum of all Total index values:
echo array_sum(
array_column(
array_column(
$array['data'],
'fieldData'
),
'Total'
)
);
Reference:- PHP: array_column - Manual

How to shift array elements from associative array by one in following scenario in PHP?

I've following associative array titled $_POST as follows:
Array
(
[op] => add
[product_id] => 12
[pack] => Array
(
[1] => 1
)
[applicable_states] => Array
(
[0] => multiselect-all
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
)
[total_count] => 3000
)
Now you can observe the first key from array $_POST['applicable_states'] it's [0] => multiselect-all.
I have to check this key before manipulating the array.
Whenever this key is present into array I need the $_POST array as below :
Array
(
[op] => add
[product_id] => 12
[pack] => Array
(
[1] => 1
)
[applicable_states] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[total_count] => 3000
)
Now you can see from above array that the [0] => multiselect-all is removed from the new resultant array and each array value is changed it's position by one. How should I convert my $_POST array into above resultant array in optimum way? Thanks in advance.
if (array_search('multiselect-all', $_POST['applicable_states']) === 0)
array_shift($_POST['applicable_states']);

Categories