根据比较同一数组中的两个数组值来取消设置数组键 [英] unset array keys based on comparing two array values in the same array
问题描述
我正在尝试找出一种简单的方法,该方法基于 php 中数组中较高的辅助值来删除/取消重复的数组值。
这是原始数组的简单示例。
$ ar = array(
array('pid'=>'544','discount'=> '26 .00','promo_id'=>'9807'),
array('pid'=>'544','discount'=> '15 .00','promo_id'=>'9821'),
array('pid'=>'544', '折扣'=> '21 .00','promo_id'=>'9811'),
array('pid'=>'2965','折扣'=> '25 .00','promo_id' =>'9810'),
数组('pid'=>'2965','discount'=> '30 .50','promo_id'=>'9809'),
数组('pid'=>'1866','discount'=> '30 .00','promo_id'=>'9810'),
array('pid'=>'1866','discount '=> '25 .50','promo_id'=>'9809')
);
这将是我想要的新数组。
$ ar = array(
array('pid'=>'544','discount'=> '26 .00','promo_id'=>'9807'),
array('pid'=>'2965','discount'=> '30 .50','promo_id'=>'9809' ),
array('pid'=>'1866','discount'=> '30 .00','promo_id'=>'9810'),
);
这些将是已删除的密钥。
// array('pid'=>'1866','discount'=> '25 .50','promo_id'=> '9809')已删除!
// array('pid'=>'544','discount'=> '21 .00','promo_id'=>'9811'),已删除!
// array('pid'=>'544','discount'=> '15 .00','promo_id'=>'9821'),已删除!
//array('pid'=>'2965','discount'=>'25.00','promo_id'=>'9810'),已删除!
我想删除'pid'的所有重复值
通过保持来获得折扣
的最高值。
数组可能有两个以上具有相同'pid'
的键,但它们永远不会具有相同的'pid'
和'promo_id'
。
希望有一个简单的解决方案。
编辑:
这就是我一直在尝试的内容。
foreach($ ar as $ tkey => $ v){
$ tPID = $ v ['pid'];
$ tDISC = $ v ['discount'];
if($ tPID){
$ key = array_search($ tPID,array_column($ ar,'pid'));
$ aPID = $ ar [$ key] [’pid’];
$ aTPRD = $ ar [$ key] [’discount’];
if($ aTPRD< $ tDISC){
echo‘(UN:’。$ tkey。’)’; unset($ ar [$ tkey]);
}否则
if($ aTPRD> $ tDISC){
echo‘(UN:’。$ key。’)’; unset($ ar [$ key]);
} else {echo‘else’; }
}
}
创建一个输出数组,键入 pid,其中包含为pid找到的最高折扣条目。
使用 array_values提取输出数组。 / p>
建议对代码进行编辑以使其更短。
但是,我尝试确保代码易于理解,而不是尝试使代码最少。无论如何,此代码非常有效。
<?php //
$ ar = array(
array(' pid'=>'544','discount'=> '26 .00','promo_id'=>'9807'),
array('pid'=>'544','discount'= > '15 .00','promo_id'=>'9821'),
array('pid'=>'544','discount'=> '21 .00','promo_id'=>' 9811'),
array('pid'=>'2965','discount'=> '25 .00','promo_id'=>'9810'),
array('pid' =>'2965','discount'=> '30 .50','promo_id'=>'9809'),
array('pid'=>'1866','discount'=> '30 .00','promo_id'=>'9810'),
array('pid'=>'1866','discount'=> '25 .50','promo_id'=>'9809' )
);
$ outUnique = array();
foreach($ ar as $ entry){
$ curPid = $ entry ['pid'];
if(isset($ outUnique [$ curPid])){//检查折扣
if($ entry ['discount']> $ outUnique [$ curPid ] ['discount']){
$ outUnique [$ curPid] = $ entry;
}
}
else {//添加到输出
$ outUnique [$ curPid] = $ entry;
}
}
//显示条目
var_dump(array_values($ outUnique));
I'm trying to figure out a simple way to remove/unset duplicate array values based off a higher secondary value in the array in php.
Here's an simple example of the original array.
$ar = array(
array('pid'=>'544', 'discount'=>'26.00','promo_id'=>'9807'),
array('pid'=>'544', 'discount'=>'15.00','promo_id'=>'9821'),
array('pid'=>'544', 'discount'=>'21.00','promo_id'=>'9811'),
array('pid'=>'2965','discount'=>'25.00','promo_id'=>'9810'),
array('pid'=>'2965','discount'=>'30.50','promo_id'=>'9809'),
array('pid'=>'1866','discount'=>'30.00','promo_id'=>'9810'),
array('pid'=>'1866','discount'=>'25.50','promo_id'=>'9809')
);
This would be the NEW array that I would like to get.
$ar = array(
array('pid'=>'544', 'discount'=>'26.00','promo_id'=>'9807'),
array('pid'=>'2965','discount'=>'30.50','promo_id'=>'9809'),
array('pid'=>'1866','discount'=>'30.00','promo_id'=>'9810'),
);
These would be the keys that have been removed.
//array('pid'=>'1866','discount'=>'25.50','promo_id'=>'9809') Removed!
//array('pid'=>'544', 'discount'=>'21.00','promo_id'=>'9811'), Removed!
//array('pid'=>'544', 'discount'=>'15.00','promo_id'=>'9821'), Removed!
//array('pid'=>'2965','discount'=>'25.00','promo_id'=>'9810'), Removed!
I would like to remove any duplicate values of the 'pid'
by KEEPING the highest value of the 'discount'
.
The array may have more than two keys that have the same 'pid'
but they would never have the same 'pid'
and 'promo_id'
.
Hopefully there is an easy solution for this.
EDIT:
Here what I've been trying.
foreach($ar as $tkey => $v) {
$tPID = $v['pid'];
$tDISC = $v['discount'];
if ($tPID) {
$key = array_search($tPID, array_column($ar, 'pid'));
$aPID = $ar[$key]['pid'];
$aTPRD = $ar[$key]['discount'];
if ($aTPRD < $tDISC) {
echo '(UN:'.$tkey.')'; unset($ar[$tkey]);
}else
if ($aTPRD > $tDISC) {
echo '(UN:'.$key.')'; unset($ar[$key]);
}else{echo 'else'; }
}
}
Create an output array, keyed on 'pid' that holds the highest discount entry found for the pid.
Extract the output array using 'array_values'.
Working code at Codepad.org...
There was a suggested edit to the code to make it shorter. However, i try to ensure the code is easy to understand rather than try and make it the minimum amount of code. This code is quite efficient as it is anyway.
<?php //
$ar = array(
array('pid'=>'544', 'discount'=>'26.00','promo_id'=>'9807'),
array('pid'=>'544', 'discount'=>'15.00','promo_id'=>'9821'),
array('pid'=>'544', 'discount'=>'21.00','promo_id'=>'9811'),
array('pid'=>'2965','discount'=>'25.00','promo_id'=>'9810'),
array('pid'=>'2965','discount'=>'30.50','promo_id'=>'9809'),
array('pid'=>'1866','discount'=>'30.00','promo_id'=>'9810'),
array('pid'=>'1866','discount'=>'25.50','promo_id'=>'9809')
);
$outUnique = array();
foreach ($ar as $entry) {
$curPid = $entry['pid'];
if (isset($outUnique[$curPid])) { // check the discount
if ($entry['discount'] > $outUnique[$curPid]['discount']) {
$outUnique[$curPid] = $entry;
}
}
else { // add to the output
$outUnique[$curPid] = $entry;
}
}
// show the entries
var_dump(array_values($outUnique));
这篇关于根据比较同一数组中的两个数组值来取消设置数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!