所以我有很多部分 在很多帮助下,我设法将相关部分与单个参考密钥相关联 因此,我有以下数据;
array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '50', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '250', 'uid' => '4', 'rid' =>'2' )
)
现在,如果你注意价格,UID和RID - 你会看到一个模式 前两个数组元素和最后两个元素(它们具有相同的相关ID)属于一起。
问题是 - 价格有分歧!
我需要一种方法来执行以下操作;
1)在阵列中添加一个标志以进行不同的定价
2)将所有价格改为最低价格
作为奖励 - 我也想创建一个“父”元素...因此我会添加两个新的子数组(一个用于RID1,一个用于IRD2),结合MDL,PART和MAKE等。)。
我自己尝试了这一点,并且通常会造成混乱 我可以预先生成并生成一个每个RID有一个子节点的新数组 - 但是我不能从原始数据中追加多个子数组的值:((我以为我越来越近,但是添加了价格值!)。
$composite = array();
foreach($data as $value) {
if(array_key_exists($value['rid'], $composite)) {
$composite[$value['rid']] += $value['price'];
//$composite[$value['rid']] = array('prices'=>$value['price']);
} else {
$composite[$value['rid']] = $value['price'];
//$composite[$value['rid']] = array('prices'=>$value['price']);
}
}
foreach($composite as $key=>$out){
echo $key ." : "; print_r($out); echo "<br/>";
}
所以...为了清楚起见......我想接受这个;
array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2' )
)
并以此结束;
array(
// New Parent (for rid=1's)
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1,prt2', 'price' => '50', 'uid' => '1', 'rid' =>'1', 'divprice' => true ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '2', 'rid' =>'1', 'divprice' => true ),
// Price changed from 150 to 50 - based on lowest price for matching rid
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '3', 'rid' =>'1', 'divprice' => true ),
// New Parent (for rid=2's)
array ( 'make' => 'co1, co2', 'model' => 'mdl2, mdl3', 'part' => 'prt3,prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2', 'divprice' => false),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '5', 'rid' =>'2', 'divprice' => false ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '6', 'rid' =>'2', 'divprice' => false )
)
我知道它看起来很复杂 - 但我在代码中看到了“类似”的功能 - 但对于我的生活,我无法使其工作......我最终合并和丢失价值,或者将它们加在一起(所以价格最终为200而不是50等。)
谢谢。
答案 0 :(得分:1)
不确定你所说的价格差异的标志是什么意思,但对于你想用最低价格更新所有价格的那一点,你可以做类似的事情:
// Your data
$products = array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2' )
);
// Get lowest product
function get_lowest_product($products) {
$lowest = 0;
foreach ($products as $key => $product) {
$price = (int)$product["price"];
if ($price < $lowest || $lowest == 0) {
$lowest = (int)$product["price"];
}
}
return $lowest;
}
// Replace value in array
function replace_all_prices_to_lowest($products, $lowest) {
foreach ($products as $key => &$product) {
$product["price"] = (string)$lowest;
}
return $products;
}
// Testing
$lowest = get_lowest_product($products);
$replaced = replace_all_prices_to_lowest($products, $lowest);
var_dump($replaced);