【问题标题】:Compare and remove from multidimensional array比较并从多维数组中删除
【发布时间】:2015-12-11 10:58:30
【问题描述】:

我有一个多维数组如下:

array(2) {
  [0]=>
  array(8) {
    ["id"]=>
    string(3) "117"
    ["promotiontype_id"]=>
    string(1) "1"
    ["groupa_id"]=>
    string(3) "390"
    ["groupb_id"]=>
    string(3) "390"
    ["varx"]=>
    string(1) "2"
    ["vary"]=>
    string(1) "1"
    ["varz"]=>
    string(0) ""
    ["totaldiscount"]=>
    float(6.5)
  }
  [1]=>
  array(8) {
    ["id"]=>
    string(3) "117"
    ["promotiontype_id"]=>
    string(1) "1"
    ["groupa_id"]=>
    string(3) "390"
    ["groupb_id"]=>
    string(3) "390"
    ["varx"]=>
    string(1) "2"
    ["vary"]=>
    string(1) "1"
    ["varz"]=>
    string(0) ""
    ["totaldiscount"]=>
    float(7.0)
  }
}

所以,如您所见,第一个数组的“totaldiscount”为 6.5,第二个数组为 7.0。

基本上,我需要删除包含最小值的数组,因此在这种情况下,将删除数组 [0],因为 6.5 具有最低的“总折扣”。该数组可以包含两个以上的子数组。

我认为这与探查有关,但我的大脑会因为这个而崩溃!

任何帮助将不胜感激!

【问题讨论】:

  • 可以按totaldiscount asc对数组进行排序,去掉第一个条目

标签: php arrays multidimensional-array foreach


【解决方案1】:

我将把你的问题分成两部分:

如何求x的最小值?

function lowestX($array){
   $lowest = 999;
   for($array as $var){
      if($lowest > $var["totaldiscount"]){
         $lowest = $var["totaldiscount"];
      }
   }
}

如何从数组中删除一个值?

使用unsset 或function.array-splice

【讨论】:

    【解决方案2】:

    一种可能的解决方案是使用usort 按“totaldiscount”对数组进行排序

    然后从排序后的数组中获取最低的“totaldiscount”值。

    然后您可以循环排序的数组(如果有多个具有相同最小值的“totaldiscount”值)并使用数组键取消设置包含$lowestTotaldiscount 的数组。

    例如:

    <?php
    function cmp($a, $b)
    {
        return ($a["totaldiscount"] < $b["totaldiscount"]) ? -1 : 1;
    }
    
    usort($arrays, "cmp");
    
    $lowestTotaldiscount = $arrays[0]["totaldiscount"];
    
    foreach($arrays as $key => $array) {
        if ($array["totaldiscount"] === $lowestTotaldiscount) {
            unset($arrays[$key]);
        }
    }
    

    Demo

    【讨论】:

      【解决方案3】:

      这是您问题的解决方案。只需使用 array_multisort & unset ,如下所示。 `

      $yourArray = array(
          array(
          "id"=>"117",
          "promotiontype_id"=>"1",
          "groupa_id"=>"390",
          "groupb_id"=> "390",
          "varx"=>"2",
          "vary"=>"1",
          "varz"=> "",
          "totaldiscount"=>8.5,
         ),
        array (
          "id"=> "117",
          "promotiontype_id"=>"1",
          "groupa_id"=>"390",
          "groupb_id"=>"390",
          "varx"=>"2",
          "vary"=>"1",
          "varz"=>"",
          "totaldiscount"=>7.0,
         ),
         array (
              "id"=> "117",
              "promotiontype_id"=>"1",
              "groupa_id"=>"390",
              "groupb_id"=>"390",
              "varx"=>"2",
              "vary"=>"1",
              "varz"=>"",
              "totaldiscount"=>9.0,   )
      );
      $discount = array();
      foreach ($yourArray as $key => $row)
      {
          $discount[$key] = $row['totaldiscount'];
      }
      array_multisort($discount, SORT_ASC, $yourArray);
      unset($yourArray[0]);
      print"<pre>";
      print_r($yourArray);`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-05
        • 1970-01-01
        • 2011-11-15
        相关资源
        最近更新 更多