douxi3977 2011-07-30 18:44
浏览 20
已采纳

连接数组,保留不同的值,选择一个相同的。 用PHP

lets say from zero to more is important level of the value

  # very imporant
  0 =>
    array
      'index' => string 'helloworld:Index' (length=16)
      404 => string 'helloworld:Missinga' (length=19)
      503 => string 'helloworld:Offline' (length=18)
      'nojs' => string 'helloworld:Nojs' (length=15)
      'blog' => string 'helloworld:blog' (length=15)
  # important
  1 => 
    array
      'index' => string 'helloworld:Index' (length=16)
      404 => string 'helloworld:Missingb' (length=19)
      503 => string 'helloworld:Offline' (length=18)
      'nojs' => string 'helloworld:Nojs' (length=15)
      'blogb' => string 'helloworld:blog' (length=15)
  # not that important
  2 => 
    array
      'index' => string 'helloworld:Index' (length=16)
      404 => string 'helloworld:Missingc' (length=19)
      503 => string 'helloworld:Offline' (length=18)
      'nojs' => string 'helloworld:Nojs' (length=15)
       'more' => string 'helloworld:Nojs' (length=15)
  # so on

join them into one array to something like this

    array
      'index' => string 'helloworld:Index' (length=16) # from 0 ( others same key )
      404 => string 'helloworld:Missinga' (length=19)  # from 0 ( others same key )
      503 => string 'helloworld:Offline' (length=18)   # from 0 ( others same key )
      'nojs' => string 'helloworld:Nojs' (length=15)   # from 0 ( others same key )
      'blog' => string 'helloworld:blog' (length=15)   # from 0 ( new )
      'blogb' => string 'helloworld:blog' (length=15)  # from 1 ( new )
      'more' => string 'helloworld:Nojs' (length=15)   # from 2 ( new )
  1. when the key is different = append
  2. when the key is same = check important level get one highest level

question what is the best way we can merge multiple into array but something like this?

thanks for looking in

Adam Ramadhan

  • 写回答

3条回答 默认 最新

  • dongshuo1257 2011-07-30 19:02
    关注

    You can do this

    $array1 + $array2 + array3;
    

    Unlike array_merge(), the one with the most importance goes first, and numeric keys are respected.

    If, like in your question, you are merging array elements in the same array, you can do

    $result = array();
    foreach ($array as $value) { $result += $value; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算