将数组元素移动到PHP中的新索引

cro*_*lee 38 php arrays

我正在寻找一个简单的函数来将数组元素移动到数组中的新位置并重新排序索引,以便序列中没有间隙.它不需要使用关联数组.有人有这个想法吗?

$a = array(
      0 => 'a',
      1 => 'c',
      2 => 'd',
      3 => 'b',
      4 => 'e',
);
print_r(moveElement(3,1))
//should output 
    [ 0 => 'a',
      1 => 'b',
      2 => 'c',
      3 => 'd',
      4 => 'e' ]
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 97

评论说,2x array_splice,甚至没有必要重新编号:

$array = [
    0 => 'a', 
    1 => 'c', 
    2 => 'd', 
    3 => 'b', 
    4 => 'e',
];

function moveElement(&$array, $a, $b) {
    $out = array_splice($array, $a, 1);
    array_splice($array, $b, 0, $out);
}

moveElement($array, 3, 1);
Run Code Online (Sandbox Code Playgroud)

结果:

[
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
];
Run Code Online (Sandbox Code Playgroud)

  • 但是如果您的目的地在源之后,它可能不会将其放置在您预测的位置,因为在第一步之后阵列会变短.如果移到最后,你不想使用size-1,你想要size-2 (5认同)
  • @quarky:不,如果新索引大于旧索引,它也可以.如果两者都相同的话. (3认同)
  • 澄清`$ a`是`$ fromIndex`而`$ b`是`$ toIndex` (3认同)

dea*_*ina 13

很多好的答案。这是一个基于@RubbelDeCatc 答案的简单答案。它的美妙之处在于您只需要知道数组键,而不是它的当前位置(在重新定位之前)。

/**
 * Reposition an array element by its key.
 *
 * @param array      $array The array being reordered.
 * @param string|int $key They key of the element you want to reposition.
 * @param int        $order The position in the array you want to move the element to. (0 is first)
 *
 * @throws \Exception
 */
function repositionArrayElement(array &$array, $key, int $order): void
{
    if(($a = array_search($key, array_keys($array))) === false){
        throw new \Exception("The {$key} cannot be found in the given array.");
    }
    $p1 = array_splice($array, $a, 1);
    $p2 = array_splice($array, 0, $order);
    $array = array_merge($p2, $p1, $array);
}
Run Code Online (Sandbox Code Playgroud)

直接使用:

$fruits = [
    'bananas'=>'12', 
    'apples'=>'23',
    'tomatoes'=>'21', 
    'nuts'=>'22',
    'foo'=>'a',
    'bar'=>'b'
];

repositionArrayElement($fruits, "foo", 1);

var_export($fruits);

/** Returns
array (
  'bananas' => '12',
  'foo' => 'a', <--  Now moved to position #1
  'apples' => '23',
  'tomatoes' => '21',
  'nuts' => '22',
  'bar' => 'b',
)
**/
Run Code Online (Sandbox Code Playgroud)

也适用于数值数组:

$colours = ["green", "blue", "red"];

repositionArrayElement($colours, 2, 0);

var_export($colours);

/** Returns
array (
  0 => 'red', <-- Now moved to position #0
  1 => 'green',
  2 => 'blue',
)
*/
Run Code Online (Sandbox Code Playgroud)

演示


Rub*_*atc 6

带有两个array_splice命令的hakre解决方案不适用于命名数组.移动元素的键将丢失.

相反,您可以将数组拼接两次并合并各个部分.

function moveElement(&$array, $a, $b) {
    $p1 = array_splice($array, $a, 1);
    $p2 = array_splice($array, 0, $b);
    $array = array_merge($p2,$p1,$array);
}
Run Code Online (Sandbox Code Playgroud)

它是如何工作的:

  • 首先:从数组中删除/拼接元素
  • 第二步:在要插入元素的位置将数组拼接成两部分
  • 将这三个部分合并在一起

例:

$fruits = array(
    'bananas'=>'12', 
    'apples'=>'23',
    'tomatoes'=>'21', 
    'nuts'=>'22',
    'foo'=>'a',
    'bar'=>'b'
);

moveElement($fruits, 1, 3);

// Result
['bananas'=>'12', 'tomatoes'=>'21', 'nuts'=>'22', 'apples'=>'23', 'foo'=>'a', 'bar'=>'b']
Run Code Online (Sandbox Code Playgroud)