【问题标题】:joining arrays, preserving different value, pick one if same. with php加入数组,保留不同的值,如果相同,则选择一个。用 php
【发布时间】:2011-07-30 18:44:59
【问题描述】:

让我们说从零到更多是价值的重要水平

  # 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

将它们加入到一个数组中

    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. 当密钥不同时 = 追加
  2. 当密钥相同时 = 检查重要级别得到一个最高级别

问题我们可以将多个合并到数组中的最佳方法是什么?

感谢关注

亚当斋月

【问题讨论】:

    标签: php arrays logic


    【解决方案1】:

    你可以这样做

    $array1 + $array2 + array3;
    

    array_merge()不同,最重要的优先,数字键优先。

    如果像您的问题一样,您将数组元素合并到同一个数组中,您可以这样做

    $result = array();
    foreach ($array as $value) { $result += $value; }
    

    【讨论】:

    • 哇不知道我们能做到这一点 + + + 它是如何工作的?我没有看到任何引用此的 php 文档。
    • 它是 PHP 中更有用但未被充分利用的运算符之一。 php.net/manual/en/language.operators.array.php
    • 太棒了,倒过来怎么样?
    • foreach之前做array_reverse($array);
    • 也不知道 - 非常棒且有用的答案!
    【解决方案2】:

    只需将array_merge 与您的三个数组一起用作参数。最重要的应该是最后一个,所以array_merge($array2, $array1, $array0) 应该可以正常工作。不过,数字键可能会产生问题:

    但是,如果数组包含数字键,则后面的值不会覆盖原始值,而是会被追加。

    您可以考虑将它们转换为字符串。

    【讨论】:

    • 它在一个数组上,我们如何拆分它们以合并它们?
    • 你的意思是你的初始数组在一个大数组中?你可以尝试这样的事情:$result = array(); array_merge($result, $array[2], $array[1], $array[0]);,或者如果你有超过三个重要性级别,则可以尝试动态的。
    【解决方案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;
         }
       }
    }
    

    这只有在“旧”数组像您的示例中那样按重要性排序时才有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多