我有一个数组。在做了一些条件后,我得到的数组如下所示。
//filtered aray
Array
(
[2] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => -1129794.96
[member_id] => 1
[type] => InternalTransfers
[transaction_type] => Accumulation
)
[13] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => 1129794.96
[member_id] => 1
[type] => PensionsRolledBack
[transaction_type] => Accumulation
)
[23] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => -1129794.96
[member_id] => 1
[type] => PensionsRolledBack
[transaction_type] => Pension
)
[24] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => 1129794.96
[member_id] => 2
[type] => InternalTransfers
[transaction_type] => Accumulation
)
[36] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => -1129794.96
[member_id] => 2
[type] => PensionCommencement
[transaction_type] => Accumulation
)
[56] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => 1129794.96
[member_id] => 2
[type] => PensionCommencement
[transaction_type] => Pension
)
)
我的工作是删除任何抵消交易。例如:如果一个成员的积累具有正面和负面,则将其删除。
通过使用此结果数组,我需要根据member_id
,transaction_type
和transaction_amount
进行过滤。 (可能是正面的也可能是负面的)。
如果member_id
和transaction_type
相同,那么我需要删除抵消交易。
删除后,生成的数组必须如下所示:
Array
(
[23] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => -1129794.96
[member_id] => 1
[type] => PensionsRolledBack
[transaction_type] => Pension
)
[56] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => 1129794.96
[member_id] => 2
[type] => PensionCommencement
[transaction_type] => Pension
)
)
我的代码
$total = $array;
array_walk($array, function($value) use(&$total){
$result = $this->multiSearch($total, array('transaction_type' =>$value['transaction_type'], 'smsf_actuarial_form_member_id' => $value['smsf_actuarial_form_member_id'], 'transaction_amount' => $value['transaction_amount']));
pr($result);exit;
});
public function multiSearch(array $array, array $pairs) {
$found = array();
foreach ($array as $aKey => $aVal) {
$coincidences = 0;
foreach ($pairs as $pKey => $pVal) {
if (array_key_exists($pKey, $aVal)) {
if(is_double($aVal[$pKey]) && is_double($pVal) && abs($aVal[$pKey]) == abs($pVal)){
$coincidences++;
} else if($aVal[$pKey] == $pVal){
$coincidences++;
}
}
}
if ($coincidences == count($pairs)) {
$found[$aKey] = $aVal;
}
}
return $found;
}
我已根据member_id,transaction_type和transaction_amount进行过滤。
[2] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => -1129794.96
[smsf_actuarial_form_member_id] => 1
[type] => InternalTransfers
[transaction_type] => Accumulation
)
[13] => Array
(
[OriginEventId] => 0152c945-e15b-48f9-8a0e-e4b9eace4731
[Description] => Pension Reversion
[transaction_date] => 2014-10-30T00:00:00
[transaction_amount] => 1129794.96
[smsf_actuarial_form_member_id] => 1
[type] => PensionsRolledBack
[transaction_type] => Accumulation
)
如果具有相同的成员ID,交易类型和交易金额,包括正面和负面,那么我需要将其删除。
流量
使用member_id,transaction_type和transaction_amount(正面和负面)过滤数组。
如果结果数组同时具有正数和负数,则将两个数组与已过滤数组取消链接。
条件动态起作用。
答案 0 :(得分:2)
要删除数组中的元素,请使用unset函数
foreach ($array as $key => $value) {
if (isset($array[$key])) {
foreach ($array as $k => $v) {
if (
$value['member_id'] === $v['member_id'] &&
$value['transaction_type'] === $v['transaction_type'] &&
$value['transaction_amount'] === -$v['transaction_amount']
) {
unset($array[$key], $array[$k]);
break;
}
}
}
}