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.
Related
I have two multidimensional associative arrays with differen items count.
Important is that I don't know which aray will have more elements (A or B)
First array (A):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 10707
[2] => 8350
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Second array (B):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
After compare these arrays (A, B) I'd like to retrive something like that:
Array A(remove productID if exists in array B):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 10707
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Array B(no changes):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Populate a flat "blacklist" array from your second array.
Loop over the first array's rows and filter the productId values against the blacklist array.
Code: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
var_export(
array_map(
fn($row) => array_replace($row, ['productID' => array_values(array_diff($row['productID'], $blacklist))]),
$a
)
);
Or if you don't mind a classic foreach(), then this may be easier to read: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
foreach ($a as &$row) {
$row['productID'] = array_values(array_diff($row['productID'], $blacklist));
}
var_export($a);
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
)
)
really need your help with this array :
Array
(
[status] => 200
[error] =>
[resource] => Array
(
[type] => stats
[data] => Array
(
[0] => Array
(
[date] => Array
(
[year] => 2015
[month] => 12
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 2.53
[average] => 1.265
)
)
[1] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 15
[average] => 7.5
)
)
[2] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => AUD
[stats] => Array
(
[count] => 1
[total] => 15
[average] => 15
)
)
[3] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => AUD
[stats] => Array
(
[count] => 7
[total] => 1419.02
[average] => 202.71714285714
)
)
[4] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => USD
[stats] => Array
(
[count] => 8
[total] => 2186.4
[average] => 273.3
)
)
[5] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => USD
[stats] => Array
(
[count] => 3
[total] => 865
[average] => 288.33333333333
)
)
[6] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => AUD
[stats] => Array
(
[count] => 19
[total] => 127279
[average] => 6698.8947368421
)
)
)
)
)
How i can get $value of [month] in the loop??
Help me please!
Do you mean you're trying to access the "month" index within date?
If so, you could try something like this:
Let's say $array is your array.
foreach($array['resource']['data'] as $data) {
echo $data['date']['month']; // Prints every month.
echo "<br/>";
}
Hope this helps.
Try array_column function (available since PHP 5.5)
// $arr is the initial array
$months = array_column($arr, 'month');
http://php.net/manual/ru/function.array-column.php
I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.
I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}