dongzouxigu12345 2018-05-16 19:03
浏览 25

是否可以组合数组?

Is it possible to merge the contents of all existing arrays, if the key is already in use - then nothing, and if not - then it must be added from other arrays, but with an empty value

  array(3) {
      [0]=>
            array(4) {
              ["attributes_3_ru-ru"] => "10"
              ["attributes_3_en-gb"] => "100"
              ["attributes_4_en-gb"] => "2222"
              ["attributes_4_ru-ru"] => ""
            }
      [1]=>
            array(2) {
              ["attributes_6_ru-ru"] => "10"
              ["attributes_6_en-gb"] => "100"
            }
        }
      [2]=>
            array(2) {
              ["attributes_4_ru-ru"] => "10"
              ["attributes_4_en-gb"] => "100"
            }
            ...n
    }

output is something like this

array(3) {
  [0]=>
        array(4) {
          ["attributes_3_ru-ru"] => "10"
          ["attributes_3_en-gb"] => "100"
          ["attributes_4_en-gb"] => "2222"
          ["attributes_4_ru-ru"] => ""
          ["attributes_6_ru-ru"] => ""
          ["attributes_6_en-gb"] => ""
        }
  [1]=>
        array(2) {
          ["attributes_6_ru-ru"] => "10"
          ["attributes_6_en-gb"] => "100"
          ["attributes_3_ru-ru"] => ""
          ["attributes_3_en-gb"] => ""
          ["attributes_4_en-gb"] => ""
          ["attributes_4_ru-ru"] => ""
        }
    }
  [2]=>
        array(2) {
          ["attributes_4_ru-ru"] => "10"
          ["attributes_4_en-gb"] => "100"
          ["attributes_6_ru-ru"] => ""
          ["attributes_6_en-gb"] => ""
          ["attributes_3_ru-ru"] => ""
          ["attributes_3_en-gb"] => ""
        }
        ...n
    }
  • 写回答

2条回答 默认 最新

  • douti8321 2018-05-16 19:12
    关注

    You should use array_merge() for this.

    $megaArray = array_merge($array1, $array2, $array3);
    

    Or if I'm oversimplifying your use case and for some reason you need to append data to $array3 you could use foreach() and in_array() instead. Something along the lines of the following (not tested).

    foreach($array1 as $key => $value){
        if(!in_array($key, $array3)){
            $array3[$key] = $value;
      }
      else{
            if($array3[$key] === ""){ // if the current array3 iteration's value is blank, use the new one
                $array3[$key] = $value;
            }
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)