How to efficiently insert elements after another known (by key or pointer) element in a PHP array?

后端 未结 6 1971
萌比男神i
萌比男神i 2021-02-07 11:01

Given an array:

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

Wi

6条回答
  •  别跟我提以往
    2021-02-07 11:35

    Generally speaking doubly linked list would be ideal for this task.

    There is a built-in implementation of that since PHP 5.3, called SplDoublyLinkedList and since PHP 5.5 it also has add method, which allows adding/inserting values in the middle.

提交回复
热议问题