dongmeng1868 2016-04-14 09:22
浏览 49

使用array_unique,我似乎已经删除了重复的记录?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?

The array looks like this

["options"]=>
array(9) {
  [0]=>
  array(8) {
    ["id"]=>
    string(2) "79"
    ["product_id"]=>
    string(2) "15"
    ["sku"]=>
    string(9) "CSR-FTC4S"
    ["status"]=>
    string(1) "1"
    ["is_default"]=>
    string(1) "0"
    ["option_price"]=>
    string(6) "35.000"
    ["sequence"]=>
    string(4) "9999"
    ["available_options"]=>
    array(3) {
      [0]=>
      array(6) {
        ["id"]=>
        string(3) "219"
        ["product_options_base_id"]=>
        string(2) "79"
        ["option_id"]=>
        string(2) "16"
        ["option_data_id"]=>
        string(1) "1"
        ["sequence"]=>
        string(4) "9999"
        ["option_data"]=>
        array(1) {
          [0]=>
          array(8) {
            ["id"]=>
            string(1) "1"
            ["admin_name"]=>
            string(19) "Five Ten C4 Stealth"
            ["name"]=>
            string(11) "Resole Type"
            ["sku"]=>
            string(5) "FTC4S"
            ["user_value"]=>
            string(25) "Five Ten C4 Stealth 5.5mm"
            ["sequence"]=>
            string(1) "0"
            ["status"]=>
            string(1) "1"
            ["option_price"]=>
            string(5) "0.000"
          }
        }
      }
      [1]=>
      array(6) {
        ["id"]=>
        string(3) "220"
        ["product_options_base_id"]=>
        string(2) "79"
        ["option_id"]=>
        string(2) "12"
        ["option_data_id"]=>
        string(1) "1"
        ["sequence"]=>
        string(4) "9999"
        ["option_data"]=>
        array(1) {
          [0]=>
          array(8) {
            ["id"]=>
            string(1) "1"
            ["admin_name"]=>
            string(7) "Express"
            ["name"]=>
            string(7) "Express"
            ["sku"]=>
            string(3) "EXP"
            ["user_value"]=>
            string(1) "1"
            ["sequence"]=>
            string(4) "9999"
            ["status"]=>
            string(1) "1"
            ["option_price"]=>
            string(6) "25.000"
          }
        }
      }
      [2]=>
      array(6) {
        ["id"]=>
        string(3) "221"
        ["product_options_base_id"]=>
        string(2) "79"
        ["option_id"]=>
        string(2) "23"
        ["option_data_id"]=>
        string(1) "1"
        ["sequence"]=>
        string(4) "9999"
        ["option_data"]=>
        array(1) {
          [0]=>
          array(8) {
            ["id"]=>
            string(1) "1"
            ["admin_name"]=>
            string(16) "Rand Toe Patches"
            ["name"]=>
            string(3) "RTP"
            ["sku"]=>
            string(3) "RTP"
            ["user_value"]=>
            string(1) "1"
            ["sequence"]=>
            string(4) "9999"
            ["status"]=>
            string(1) "1"
            ["option_price"]=>
            string(6) "10.000"
          }
        }
      }
    }
  }
  [1]=>
  array(8) {
    ["id"]=>
    string(2) "80"
    ["product_id"]=>
    string(2) "15"
    ["sku"]=>
    string(10) "CSR-FTONYX"
    ["status"]=>
    string(1) "1"
    ["is_default"]=>
    string(1) "0"
    ["option_price"]=>
    string(6) "37.000"
    ["sequence"]=>
    string(4) "9999"
    ["available_options"]=>
    array(3) {
      [0]=>
      array(6) {
        ["id"]=>
        string(3) "222"
        ["product_options_base_id"]=>
        string(2) "80"
        ["option_id"]=>
        string(2) "16"
        ["option_data_id"]=>
        string(1) "2"
        ["sequence"]=>
        string(4) "9999"
        ["option_data"]=>
        array(1) {
          [0]=>
          array(8) {
            ["id"]=>
            string(1) "2"
            ["admin_name"]=>
            string(13) "Five Ten Onyx"
            ["name"]=>
            string(11) "Resole Type"
            ["sku"]=>
            string(6) "FTONYX"
            ["user_value"]=>
            string(19) "Five Ten Onyx 4.5mm"
            ["sequence"]=>
            string(1) "1"
            ["status"]=>
            string(1) "1"
            ["option_price"]=>
            string(5) "0.000"
          }
        }
      }
      [1]=>
      array(6) {
        ["id"]=>
        string(3) "223"
        ["product_options_base_id"]=>
        string(2) "80"
        ["option_id"]=>
        string(2) "12"
        ["option_data_id"]=>
        string(1) "1"
        ["sequence"]=>
        string(4) "9999"
        ["option_data"]=>
        array(1) {
          [0]=>
          array(8) {
            ["id"]=>
            string(1) "1"
            ["admin_name"]=>
            string(7) "Express"
            ["name"]=>
            string(7) "Express"
            ["sku"]=>
            string(3) "EXP"
            ["user_value"]=>
            string(1) "1"
            ["sequence"]=>
            string(4) "9999"
            ["status"]=>
            string(1) "1"
            ["option_price"]=>
            string(6) "25.000"
          }
        }
      }

and my code goes like this

        foreach($this->_data as &$data) {

        foreach($data['options'] as &$option) {

            $option['available_options'] = array_unique($option['available_options']);

        }

    }

It's working apart from it's stripped out the duplicates rather than showing them once?

  • 写回答

1条回答 默认 最新

  • douxue4242 2016-04-14 09:58
    关注

    array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.

    foreach($this->_data as &$data) {
        foreach ($data['options'] as &$option) {
            foreach ($option['available_options'] as &$available_option) {
                foreach ($available_option['option_data'] as &$option_data) {
                    $option_data = array_unique($option_data);
                }
            }
        }
    }
    

    This way, the last option_data looks like

    'option_data' => [
        [
            'id'           => '1',
            'admin_name'   => 'Express',
            'sku'          => 'EXP',
            'sequence'     => '9999',
            'option_price' => '25.000'
        ]
    ]
    

    But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入