开发者

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

开发者 https://www.devze.com 2023-03-23 20:19 出处:网络
lets say from zero to more is important level of the value # very imporant 0 => array \'index\' => string \'helloworld:Index\' (length=16)

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:Mi开发者_开发问答ssingc' (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


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; }


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.


$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号