dongqiao8502 2019-05-04 04:49
浏览 41
已采纳

使用PHP更改数组中重复项的值

I have array

`array(
   [0] =>array(
     [id] => 1,
     [item] => ring,
     [total] => 1000
   ),
   [1] =>array(
     [id] => 1,
     [item] => book,
     [total] => 1000
   ),
   [2] =>array(
     [id] => 1,
     [item] => pen,
     [total] => 400
   )
);`

I need result when [id] is same value only first to show [total] and other need to show - like below please.

 `array(
  [0] =>array(
     [id] => 1,
     [item] => ring,
     [total] => 1000
   ),
  [1] =>array(
     [id] => 1,
     [item] => book,
     [total] => -
   ),
  [2] =>array(
     [id] => 1,
     [item] => pen,
     [total] => 400
   )
  );`

Thank you for help please.

  • 写回答

1条回答 默认 最新

  • duanhuang2150 2019-05-04 05:41
    关注

    This script might help you to do so:

    $arr = [
        "0" => [
            "id" => "1",
            "item" => "ring",
            "total" => "1000",
        ],
        "1" => [
            "id" => "1",
            "item" => "book",
            "total" => "1000",
        ],
        "2" => [
            "id" => "1",
            "item" => "pen",
            "total" => "400",
        ],
        "3" => [
            "id" => "1",
            "item" => "pen",
            "total" => "400",
        ],
        "4" => [
            "id" => "1",
            "item" => "pen",
            "total" => "400",
        ],
        "5" => [
            "id" => "1",
            "item" => "pen",
            "total" => "500",
        ],
        "6" => [
            "id" => "1",
            "item" => "ring",
            "total" => "1000",
        ],
    ];
    
    $out_arr = array();
    foreach ($arr as $key => $value) {
        array_push($out_arr, $value);
        if ($arr[(int) $key + 1]["total"] && $arr[(int) $key + 1]["id"]) {
            foreach ($arr as $key2 => $value2) {
                if ($value["id"] == $arr[(int) $key2 + 1]["id"] && $value2["total"] == $arr[(int) $key2 + 1]["total"]) {
                    $arr[(int) $key + 1]["total"] = '-';
                }
            }
    
        }
    }
    
    var_dump($arr);
    

    Output

    array(7) {
      [0]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(4) "ring"
        ["total"]=>
        string(4) "1000"
      }
      [1]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(4) "book"
        ["total"]=>
        string(1) "-"
      }
      [2]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(3) "pen"
        ["total"]=>
        string(1) "-"
      }
      [3]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(3) "pen"
        ["total"]=>
        string(1) "-"
      }
      [4]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(3) "pen"
        ["total"]=>
        string(1) "-"
      }
      [5]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(3) "pen"
        ["total"]=>
        string(1) "-"
      }
      [6]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["item"]=>
        string(4) "ring"
        ["total"]=>
        string(1) "-"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条