Предположим, у меня есть этот массив
[0] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000001
[gross] => 34650000
[vat] => 3465000
)
[1] => Array
(
[revision_no] => 1
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000001
[gross] => 44650000
[vat] => 4465000
)
[2] => Array
(
[revision_no] => 2
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000001
[gross] => 34650000
[vat] => 3465000
)
[3] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000002
[gross] => 34650000
[vat] => 3465000
)
[4] => Array
(
[revision_no] => 1
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000002
[gross] => 34650000
[vat] => 3465000
)
[5] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000003
[gross] => 34650000
[vat] => 3465000
)
[6] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 2132134923102931
[document_number] => 010.000-12.00000003
[gross] => 34650000
[vat] => 3465000
)
Что я должен делать в PHP, чтобы выбрать уникальные на основе revision_no
vendor_no
а также document_number
, И тогда для тех, кто имеет то же самое vendor_no
а также document_number
бери только тот, у кого самый высокий revision_no
,
Так что результат будет такой:
[2] => Array
(
[revision_no] => 2
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000001
[gross] => 34650000
[vat] => 3465000
)
[3] => Array
(
[revision_no] => 1
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000002
[gross] => 34650000
[vat] => 3465000
)
[5] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 311560353071000
[document_number] => 010.000-12.00000003
[gross] => 34650000
[vat] => 3465000
)
[6] => Array
(
[revision_no] => 0
[invoice_type] => PK
[transaction_id] => 5
[vendor_number] => 2132134923102931
[document_number] => 010.000-12.00000003
[gross] => 34650000
[vat] => 3465000
)
$output = array_reduce(
$input,
function (array $carry, array $item) {
// generate the key to identify the duplicates
// Add $item['gross'] if needed
$key = $item['vendor_number'].'/'.$item['document_number'];
// If this is the first appearance of the key
// then add the value to the partial list and return it
if (! isset($carry[$key])) {
$carry[$key] = $item;
return $carry;
}
// A previous revision exists
// Check values in $item against those already existing in the list
$old = $carry[$key];
if ($old['revision_no'] < $item['revision_no']) {
// This is a new revision, replace the old one
$carry[$key] = $item;
}
// Return $carry (updated or not)
return $carry;
},
array()
);
Этот код не сохраняет ключи из исходного массива. Решение, которое сохраняет ключи, может быть реализовано аналогичным образом с использованием array_walk()
.
Других решений пока нет …