joining arrays, preserving different value, pick one if same. with php

回眸只為那壹抹淺笑 提交于 2019-12-08 04:07:43

问题


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


回答1:


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:


Just use array_merge with your three arrays as parameters. The most important one should be the last one, so array_merge($array2, $array1, $array0) should work fine. The numeric keys might make a problem, though:

If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

You might consider converting them to strings.




回答3:


$new_array = array();

 foreach($old_array as $level => $key_array) {
   foreach($key_array as $key => $value) {
     if(!isset($new_array[$key])) {
       $new_array[$key] = $value;
     }
   }
}

This will only work, if the "old" array is sorted by importance like in your example.



来源:https://stackoverflow.com/questions/6885243/joining-arrays-preserving-different-value-pick-one-if-same-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!