PHP多维数组排序&删除行

时间:2012-02-23 14:56:00

标签: php multidimensional-array associative-array

我有一个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

4 个答案:

答案 0 :(得分:4)

您的任务实际上并不需要排序。你只是在寻找最低的项目(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);

修改:修改了我的回答。这将创建一个关联数组,其中键是类型,值是具有最低价格的原始数组。

祝你好运!

答案 1 :(得分:2)

编辑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];

答案 2 :(得分:2)

<?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";
}

答案 3 :(得分:0)

让米哈伊尔更进一步,希望更符合你的要求:

    $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] [&#34; rose&#34;] [&#34; blue&#34;] [&#34; large&#34;] [&#34; 4.99&#34;] =无论;

我建议将其更改为:

$ flower [0] =&gt;数组(&#34;类型&#34; =&gt;&#34;玫瑰&#34;,&#34;颜色&#34; =&gt;&#34;蓝&#34;,&#34;尺寸&#34; =&gt;&#34;大&#34;,&#34;价格&#34; =&gt;&#34; 4.99&#34;);