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

后端 未结 6 1968
萌比男神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:53

    You can't use internal array pointer to insert elements.

    There's array_splice which can insert/remove/replace elements and subarrays, but it's intended for integer-indexed arrays.

    I'm afraid you'll have to rebuild the array to insert element (except cases where you want to insert first/last element) or use separate integer-indexed array for holding keys in the order you want.

提交回复
热议问题