PHP - 提取数组的前 3 个元素 -> 打乱它 -> 并将它们添加回 orig。大批

PHP - Extract first 3 elements of an array -> Shuffle it -> and add them back to the orig. array

是否可以 "extract" 数组的前 3 个元素,然后打乱这 3 个元素并将它们添加回数组?

这是一个滑块,前 3 张幻灯片应该在每次页面加载时随机显示 ...

有人可以帮忙吗?

 public function shuffle( $data ) {

    // Return early there are no items to shuffle.
    if ( ! is_array( $data['slider'] ) ) {
        return $data;
    }

    // Prepare variables.
    $random = array();
    $keys   = array_keys( $data['slider'] );

    // Shuffle the keys and loop through them to create a new, randomized array of images.
    shuffle( $keys );
    foreach ( $keys as $key ) {
        $random[$key] = $data['slider'][$key];
    }

    // Return the randomized image array.
    $data['slider'] = $random;
    return $data;

}

/* ---------------------- 更新 ------------------ ---- */

这就是我的工作方式,但为什么呢?我对 php 比较陌生 ;D

public function shuffle($data) {

    // Return early there are no items to shuffle.
    if (!is_array($data['slider'])) {
        return $data;
    }

    $sliced_array = array_slice($data["slider"], 0, 3, TRUE);
    // Shuffle the keys and loop through them to create a new, randomized array of images.
    shuffle($sliced_array);


    $data['slider'] = $sliced_array + array_slice($data["slider"], 0);

    return $data;
}
<?php 
$data = [1,2,3,4,5,6,7,8,9];

//get 3 first elements and remove 3 first elements from main array
$remains = array_splice($data,3);

//shuffle 3 elements
shuffle($data);

//join everything back
$data = array_merge(array_values($data), array_values($remains));

是的,有可能。你走在正确的轨道上。通过一些调整,它运行良好。

代码:

public function shuffling($data) {

    // Return early there are no items to shuffle.
    if (!is_array($data['slider'])) {
        return $data;
    }

    $sliced_array = array_slice($data["slider"], 0, 3, TRUE);
    // Shuffle the keys and loop through them to create a new, randomized array of images.
    shuffle($sliced_array);

    foreach ($sliced_array as $key => $value) {
        $data['slider'][$key] = $value;
    }
    return $data;
}

我试过像这样的示例数组:

shuffling(["slider" => [
        0 => "A",
        1 => "B",
        2 => "C",
        3 => "D",
        4 => "E",
]]);

结果是:

Array
(
    [slider] => Array
        (
            [0] => B
            [1] => C
            [2] => A
            [3] => D
            [4] => E
        )

)

注意:shuffle 已经是 php 中定义的函数。这就是为什么我将名称更改为 shuffling.

这是一个演示:https://3v4l.org/rYfV2

public function shuffler($data) {

    // Return early there are no items to shuffle.
    if (!is_array($data['slider'])) {
        return $data;
    }

    // Generate a copy of first three elements.
    $first = array_slice($data['slider'], 0, 3);

    // Shuffle first three elements.
    shuffle($first);

    // Overwrite original first three elements.
    $data['slider'] = array_replace($data['slider'], $first);
    // $data['slider'] = $first + $data['slider'];  // function-less alternative

    // Return the randomized image array.
    return $data;

}

array_replace() 避免以更冗长/迭代的方式进行替换过程。当您只想处理前三个元素时,它还可以避免处理整个数组。在任何时候都没有必要调用 array_values().

您也可以使用联合运算符代替 array_replace()——请参阅我在 array_replace() 行下的注释代码行。 (Demo)

来自PHP Documentation Array Operator

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.