我有一个PHP多维关联数组,其结构如下:
Fields: TYPE - COLOUR - SIZE - PRICE
[0] - rose, var2, var3, price
[1] - rose, var2, var3, price
[2] - daffodil, var2, var3, price
[3] - tulip, var2, var3, price
[4] - rose, var2, var3, price
[5] - tulip, var2, var3, price
[6] - daffodil, var2, var3, price
我想遍历这个数组,对于每个类型,我想选择价格最低的一个,并删除该类型的所有其他行。钥匙不必保存。
最后,我想要一个每种类型只有一行的数组。
我对如何做到这一点感到困惑,所以我感激地收到了任何建议。
编辑:抱歉,我的数组可能已经足够清楚了,下面是它的另一个片段:
Fields: TYPE - COLOUR - SIZE - PRICE
[0] - type=>rose, colour=>var2, size=>var3, price=>£price
[1] - type=>rose, colour=>var2, size=>var3, price=>£price
[2] - type=>daffodil, colour=>var2, size=>var3, price=>£price
[3] - type=>tulip, colour=>var2, size=>var3, price=>£price
[4] - type=>rose, colour=>var2, size=>var3, price=>£price
[5] - type=>tulip, colour=>var2, size=>var3, price=>£price
[6] - type=>daffodil, colour=>var2, size=>var3, price=>£price
您的任务实际上并不需要排序。你只是在搜索最低的项目(每种类型的adj.!)——这可以(仍然!)在O(n)中完成。
$cheapest = array();
foreach ($flower as $id => $f) {
$c_price = $cheapest[$f['type']]['price'];
if (!$c_price || $f['price'] < $c_price) {
$cheapest[$f['type']] = $f;
}
}
print_r($cheapest);
编辑:修改了我的答案。这将创建一个关联数组,其中键是类型,值是价格最低的原始数组。
祝你好运!
Mikhail解决方案的编辑版本,以返回OP要求的内容。
$minid = array();
$minPrice = array();
foreach ($flower as $id => $f) {
if (!isset($minPrice[$f['type']]) || $f['price'] < $minPrice[$f['type']]) {
$minPrice[$f['type']] = $f['price'];
$minid[$f['type']] = $id;
}
}
$filtered = array();
foreach ($minid as $id)
$filtered[] = $flower[$id];
<?php
// First, let's define the data we'll be working through.
$flowers = array(
array('type'=>'rose', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£9.99' ),
array('type'=>'rose', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£8.67'),
array('type'=>'daffodil', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£16.04'),
array('type'=>'tulip', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£4.39' ),
array('type'=>'rose', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£12.49'),
array('type'=>'tulip', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£4.49' ),
array('type'=>'daffodil', 'colour'=>'var2', 'size'=>'var3', 'price'=>'£11.99'),
);
// Define the array which will store our final flower prices
$flower_prices = array();
// Loop through our flowers to find the lowest price.
foreach($flowers as $flower) {
// Remove the pound from the price, and cast as a floating point decimal.
$price = (float) str_replace('£', '', $flower['price']);
if(!array_key_exists($flower['type'], $flower_prices)) {
// First flower type encounter. By default, as of right now, this is the "cheapest".
$flower_prices[$flower['type']] = $flower;
} else if($price < (float) str_replace('£', '', $flower_prices[$flower['type']]['price'])) {
// Flower type previously encountered, and the now encountered flower is cheaper.
$flower_prices[$flower['type']] = $flower;
}
}
// Now that we've determined which flowers are cheapest, we can print the results:
foreach($flower_prices as $price) {
print $price['type'] . ': ' . $price['price'] . "'n";
}
将Mikhail的更进一步,希望能更符合您的要求:
$selectedRows = array();
foreach ($flower as $id => $f) {
if( ! isset( $selectedRows[$f['type']] ) ) {
$selectedRows[$f['type']]['id'] = $f['id'];
$selectedRows[$f['type']]['price'] = $f['price'];
} else {
if ( $f['price'] < $selectedRows[$f['type']]['price'] ) {
$selectedRows[$f['type']]['price'] = $f['price'];
$selectedRows[$f['type']]['id'] = $f['id'];
}
}
}
// Now you can look through the selectedRows, use the
foreach( $selectedRows as $v ){
echo $flower[$v['id']].' $'.$v['price'].'<br />';
}
此外,如果你的阵列是这样的:
$flower[0]["玫瑰"]["蓝色"]["大号"]["4.99"]=随便什么;
我建议将其更改为:
$flower[0]=>数组("type"=>"rose"、"color"=>"blue"、"size"=>"large"、"price"=>"4.99");