So I have a huge array of parts.
With a lot of help, I managed to associate related parts with a singular reference key.
Thus I have the following data;
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' )
)
Now, if you pay attention to Price, UID and RID - you will see a pattern.
The first two array elements belong together, as do the last 2 elements (they have the same Related ID).
The problem is - the prices are divergent!
I need a way to do the following;
1) add a flag in the array for divergent pricing
2) change all prices to the lowest one
As a bonus - I'm also looking to create a "parent" element ... thus I'd add two new sub-arrays (one for RID1 and one for IRD2), combining the values of MDL, PART and MAKE etc.).
I've attempted this myself, and generally created a muddle.
I can foreach over and generate a new array with 1 child per RID - but I cannot append the values of multiple child arrays from the original :( (I thought I was getting close, but added the price values!).
$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/>";
}
So ... for the sake of clarity ... I'd like to take this;
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' )
)
and end up with this;
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 )
)
I know it looks complicated - but I've seen "similar" functionality in code - but for the life of me I cannot get any of it to work ... I end up merging and losing values, or adding them together (so the price ends up as 200 instead of 50 etc.)
Thank you.