【问题标题】:How to efficiently insert elements after another known (by key or pointer) element in a PHP array?如何在 PHP 数组中的另一个已知(通过键或指针)元素之后有效地插入元素?
【发布时间】:2010-11-20 04:29:18
【问题描述】:

给定一个数组:

$a = array(
    'abc',
    123,
    'k1'=>'v1',
    'k2'=>'v2',
    78,
    'tt',
    'k3'=>'v3'
);

如果它的内部指针位于其中一个元素上,我如何在当前元素之后插入一个元素? 以及如何在已知键元素之后插入一个元素,比如“k1”?

性能护理~

【问题讨论】:

    标签: php performance arrays


    【解决方案1】:

    您可以使用array_keysarray_values 拆分数组,然后将它们拼接起来,然后再次组合它们。

    $insertKey = 'k1';
    
    $keys = array_keys($arr);
    $vals = array_values($arr);
    
    $insertAfter = array_search($insertKey, $keys) + 1;
    
    $keys2 = array_splice($keys, $insertAfter);
    $vals2 = array_splice($vals, $insertAfter);
    
    $keys[] = "myNewKey";
    $vals[] = "myNewValue";
    
    $newArray = array_merge(array_combine($keys, $vals), array_combine($keys2, $vals2));
    

    【讨论】:

    • 此解决方案存在一次性错误。可以通过添加 $insertAfter++ 来纠正;在 array_splice() 调用之前。
    【解决方案2】:

    我找到了一个很好的答案here,效果很好。我想记录它,以便 SO 上的其他人可以轻松找到它:

    /*
     * Inserts a new key/value before the key in the array.
     *
     * @param $key
     *   The key to insert before.
     * @param $array
     *   An array to insert in to.
     * @param $new_key
     *   The key to insert.
     * @param $new_value
     *   An value to insert.
     *
     * @return
     *   The new array if the key exists, FALSE otherwise.
     *
     * @see array_insert_after()
     */
    function array_insert_before($key, array &$array, $new_key, $new_value) {
      if (array_key_exists($key, $array)) {
        $new = array();
        foreach ($array as $k => $value) {
          if ($k === $key) {
            $new[$new_key] = $new_value;
          }
          $new[$k] = $value;
        }
        return $new;
      }
      return FALSE;
    }
    
    /*
     * Inserts a new key/value after the key in the array.
     *
     * @param $key
     *   The key to insert after.
     * @param $array
     *   An array to insert in to.
     * @param $new_key
     *   The key to insert.
     * @param $new_value
     *   An value to insert.
     *
     * @return
     *   The new array if the key exists, FALSE otherwise.
     *
     * @see array_insert_before()
     */
    function array_insert_after($key, array &$array, $new_key, $new_value) {
      if (array_key_exists ($key, $array)) {
        $new = array();
        foreach ($array as $k => $value) {
          $new[$k] = $value;
          if ($k === $key) {
            $new[$new_key] = $new_value;
          }
        }
        return $new;
      }
      return FALSE;
    }
    

    【讨论】:

    • 有用的代码,我只是修改了它以在key不存在的情况下增加价值。
    【解决方案3】:

    您不能使用内部数组指针来插入元素。

    array_splice 可以插入/删除/替换元素和子数组,但它适用于整数索引数组。

    恐怕您必须重建数组以插入元素(除了要插入第一个/最后一个元素的情况)或使用单独的整数索引数组按您想要的顺序保存键。

    【讨论】:

      【解决方案4】:

      一般来说,双向链表是这个任务的理想选择。

      自 PHP 5.3 起有一个内置实现,称为 SplDoublyLinkedList,自 PHP 5.5 起它也有 add method,它允许在中间添加/插入值。

      【讨论】:

      • 实际上,SplDoublyLinkedList 允许使用SplDoublyLinkedList::add method 插入所需的索引,因为这是pull request。我不知道这是什么时候添加的,因为文档没有提到这是在哪个 php 版本中更改的。
      • 它确实提到了版本:(PHP 5 >= 5.5.0) - 我已经更新了答案以反映这一点。
      【解决方案5】:

      这种方式适用于没有键的新值。您不能使用键插入值,数字索引将被“重置”为 0 到 N-1。

      $keys = array_keys($a);
      $index = array_flip($keys);
      
      $key = key($a); //current element
      //or 
      $key = 'k1';
      
      array_splice($a, $index[$key] + 1, 0, array('value'));
      

      【讨论】:

        【解决方案6】:

        这里有一个很好的函数可以帮助你:https://gist.github.com/scribu/588429

        【讨论】:

          猜你喜欢
          • 2016-09-27
          • 2016-12-12
          • 2013-09-21
          • 2011-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-20
          相关资源
          最近更新 更多