suchen

Heim  >  Fragen und Antworten  >  Hauptteil

php - Bitten Sie um Rat: Wie füge ich zweidimensionale Arrays zusammen, wenn die beiden Felder „store_id“ und „source“ identisch sind?

array(5) {
  [0]=>
  array(14) {
    ["id"]=>
    string(3) "143"
    ["store_id"]=>
    string(3) "504"
    ["goodsid"]=>
    string(2) "15"
    ["source"]=>
    string(1) "2"
  }
  [1]=>
  array(14) {
    ["id"]=>
    string(3) "142"
    ["store_id"]=>
    string(3) "503"
    ["goodsid"]=>
    string(2) "33"
    ["source"]=>
    string(1) "2"
  }
  [2]=>
  array(14) {
    ["id"]=>
    string(3) "141"
    ["store_id"]=>
    string(3) "638"
    ["goodsid"]=>
    string(2) "41"
    ["source"]=>
    string(1) "1"
  }
  [3]=>
  array(14) {
    ["id"]=>
    string(3) "140"
    ["store_id"]=>
    string(3) "638"
    ["goodsid"]=>
    string(2) "41"
    ["source"]=>
    string(1) "1"
  }
}

Wie kann ein solches Array zusammengeführt werden, wenn die beiden Felder „store_id“ und „source“ gleich sind?

过去多啦不再A梦过去多啦不再A梦2819 Tage vor770

Antworte allen(3)Ich werde antworten

  • 三叔

    三叔2017-06-10 09:49:30

    没有现成的函数。只能自己写了,而且不懂你说的合并什么意思。。。
    可以循环下数据。
    新数据使用$store_id.'_'.$source这两个字段当key
    $data[$store_id.'_'.$source][] = $_data;
    这样,或者你想怎么处理

    Antwort
    0
  • 漂亮男人

    漂亮男人2017-06-10 09:49:30

    不想寫代碼,只說想法
    1.既然合併,假如id、goodsid不能被覆蓋掉,只能用三層結構數組即store_id、source為父類為一層,id、goodsid子類一層。
    2.兩個字段相同store_id、source合併,即將兩個字段合併未新的數組作為鍵值、假如有新的元素,就重新添加所有信息,假如不是,直接添加子元素

    Antwort
    0
  • ringa_lee

    ringa_lee2017-06-10 09:49:30

    使用hashmap的映射的方式:
    1.构造一个hashmap形式的数据结构,将store_id,source组合当做一个key,用可变数组作为value,将store_id和source对应的整个数据存入可变数组.
    2.便利二维数组,如果通过store_id,source组合出来的key已经存在于构造的数据结构,则将store_id和source对应的整个数据放入key对应的可变数组中,如果不存在,则加入一个新的键值对,同样是source和store_id组合值作为key,可变数组作为value。将source,store_id对应的整个数据放入key对应的可变数组。
    3.便利hashmap,合并每个键对应的数组中的数据。

    Antwort
    0
  • StornierenAntwort