Move Value in PHP Array to the Beginning of the Array

前端 未结 12 1386
终归单人心
终归单人心 2020-12-10 02:52

I have a PHP array similar to this:

0 => \"red\",
1 => \"green\",
2 => \"blue\",
3 => \"yellow\"

I want to move yellow to index

12条回答
  •  执笔经年
    2020-12-10 03:40

    PHP: move any element to the first or any position:

    $sourceArray = array(
        0 => "red",
        1 => "green",
        2 => "blue",
        3 => "yellow"
    );
    // set new order
    $orderArray = array(
        3 => '',
        1 => '',
    );
    $result = array_replace($orderArray, $sourceArray);
    print_r($result);
    

提交回复
热议问题