2

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.

4

1 回答 1

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);
于 2013-11-07T11:21:21.290 回答