Hello I have this array:
Array
(
[2018/03/01] => Array
(
[0] => Array
(
[BIL_Date] => 2018/03/01
[BIL_RateNonTaxed] => 105.00
[BIL_RateTaxed] => 115.500000000
[BIL_Status] => notcharged
)
[1] => Array
(
[BIL_Date] => 2018/03/01
[BIL_RateNonTaxed] => 60.00
[BIL_RateTaxed] => 63.000000000
[BIL_Status] => charged
)
[2] => Array
(
[BIL_Date] => 2018/03/01
[BIL_RateNonTaxed] => 21.00
[BIL_RateTaxed] => 25.194750000
[BIL_Status] => notcharged
)
)
)
How can I merge some datas to have something like this ?
I search to group by date, item charged
and notcharged
.
Array
(
[2018/03/01] => Array
(
[notcharged] => Array
(
[BIL_RateNonTaxed] => 126.00
[BIL_RateTaxed] => 140.69475
)
[charged] => Array
(
[BIL_RateNonTaxed] => 60.00
[BIL_RateTaxed] => 63.000000000
)
)
)
Here what I tried:
$datas = array();
foreach ($array as $key => $element) {
$datas[$key][] = "BIL_RateNonTaxed" => $key['BIL_RateNonTaxed'];
$datas[$key][] = "BIL_RateTaxed" => $key['BIL_RateTaxed'];
}
Could you please help me with this please ?
Thanks.
Here's the code:
foreach ($array as $key => $element) {
// init every date with empty values
$datas[$key][] = [
'notcharged' => [
'BIL_RateNonTaxed' => 0,
'BIL_RateTaxed' => 0,
],
'charged' => [
'BIL_RateNonTaxed' => 0,
'BIL_RateTaxed' => 0,
],
];
// iterate over `$element`
foreach ($element as $item) {
$datas[$key][$item['BIL_Status']]['BIL_RateNonTaxed'] += $item['BIL_RateNonTaxed'];
$datas[$key][$item['BIL_Status']]['BIL_RateTaxed'] += $item['BIL_RateTaxed'];
}
}
You can loop thru the array and use array_reduce
to summarize the data.
$array = array
(
"2018/03/01" => array
(
array
(
"BIL_Date" => '2018/03/01',
"BIL_RateNonTaxed" => '105.00',
"BIL_RateTaxed" => '115.500000000',
"BIL_Status" => 'notcharged'
),
array
(
"BIL_Date" => '2018/03/01',
"BIL_RateNonTaxed" => '60.00',
"BIL_RateTaxed" => '63.000000000',
"BIL_Status" => 'charged'
),
array
(
"BIL_Date" => '2018/03/01',
"BIL_RateNonTaxed" => '21.00',
"BIL_RateTaxed" => '25.194750000',
"BIL_Status" => 'notcharged'
)
),
);
$result = array();
foreach ($array as $key => $element) {
$result[ $key ] = array_reduce( $element, function ($c, $v) {
if( !isset( $c[ $v["BIL_Status"] ] ) ) {
$c[ $v["BIL_Status"] ] = array(
"BIL_RateNonTaxed" => $v["BIL_RateNonTaxed"],
"BIL_RateTaxed" => $v["BIL_RateTaxed"],
);
} else {
$c[ $v["BIL_Status"] ] = array(
"BIL_RateNonTaxed" => ( $c[ $v["BIL_Status"] ]["BIL_RateNonTaxed"] + $v["BIL_RateNonTaxed"] ),
"BIL_RateTaxed" => ( $c[ $v["BIL_Status"] ]["BIL_RateTaxed"] + $v["BIL_RateTaxed"] ),
);
}
return $c;
}, array() );
}
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[2018/03/01] => Array
(
[notcharged] => Array
(
[BIL_RateNonTaxed] => 126
[BIL_RateTaxed] => 140.69475
)
[charged] => Array
(
[BIL_RateNonTaxed] => 60.00
[BIL_RateTaxed] => 63.000000000
)
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.