dongmieqiao3152
2012-07-28 18:49
浏览 430
已采纳

将带有某个键的数组项移动到数组中的第一个位置PHP

What's the most elegant way in PHP to move an array element chosen by key to the first position?

Input:

$arr[0]=0;
$arr[1]=1;
$arr[2]=2;
....
$arr[n]=n;
$key=10;

Output:

$arr[0]=10;
$arr[1]=0;
$arr[2]=1;
$arr[3]=2;
....
$arr[n]=n;

图片转代码服务由CSDN问答提供 功能建议

PHP中最优雅的方法是将按键选择的数组元素移动到第一个位置?

输入:

 <代码> $ ARR [0] = 0; 
 $的ARR [1] = 1; 
 $的ARR [2] = 2  ; 
 .... 
 $的ARR [N] = N; 
 $的键= 10; 
   
 
 

输出: \ ñ

 <代码> $ ARR [0] = 10; 
 $的ARR [1] = 0; 
 $的ARR [2] = 1; 
 $的ARR [3] = 2; 
 ...  。
 $ arr [n] = n; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • dongtigai3875 2016-07-27 20:02
    已采纳

    No need to unset keys. To keep it short just do as follow

    //appending $new in our array 
    array_unshift($arr, $new);
    //now make it unique.
    $final = array_unique($arr);
    

    Demo

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzhi2017 2012-07-28 18:56
    <?php
    $key = 10;
    $arr = array(0,1,2,3);
    array_unshift($arr,$key);
    var_dump($arr) //10,0,1,2,3
    ?>
    
    评论
    解决 无用
    打赏 举报
  • drvpvmz16241016 2012-07-28 18:57

    Use array_unshift:

    $new_value = $arr[n];
    unset($arr[n]);
    array_unshift($arr, $new_value);
    
    评论
    解决 无用
    打赏 举报
  • duanchuopi2298 2012-07-28 19:01

    Something like this should work. Check if the array key exists, get its value, then unset it, then use array_unshift to create the item again and place it at the beginning.

    if(in_array($key, $arr)) {
        $value = $arr[$key];
        unset($arr[$key]);
        array_unshift($arr, $value);
    }
    
    评论
    解决 无用
    打赏 举报
  • douzao1119 2012-07-28 19:11
    $arr[0]=0;
    $arr[1]=1;
    $arr[2]=2;
    $arr[3]=10;
    
    
    $tgt = 10;
    $key = array_search($tgt, $arr);
    unset($arr[$key]);
    array_unshift($arr, $tgt);
    
    // var_dump( $arr );
    array
    0 => int 10
    1 => int 0
    2 => int 1
    3 => int 2
    
    评论
    解决 无用
    打赏 举报
  • dsubq24666 2012-07-28 20:32
    $tgt = 10;
    $key = array_search($tgt, $arr);
    for($i=0;$i<$key;$i++)
    {
       $temp = $arr[$i];
       $arr[$i] = $tgt;
       $tgt = $temp;
    }
    

    After this simple code, all you need to do is display the re-arranged array. :)

    评论
    解决 无用
    打赏 举报
  • dongyue9864 2013-07-29 19:08

    Old question, and already answered, but if you have an associative array you can use array_merge.

    $arr = array_merge([$key=>$arr[$key]], $arr);
    

    EDITED (above to show PHP7+ notation, below is example)

    $arr = ["a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d"];
    $arr = array_merge(["c"=>$arr["c"]], $arr);
    

    The effective outcome of this operation

    $arr == ["c"=>"c", "a"=>"a", "b"=>"b", "d"=>"d"]
    
    评论
    解决 无用
    打赏 举报
  • dongsheng1698 2015-12-04 09:17

    Since any numeric key would be re-indexed with array_unshift (as noted in the doc), it's better to use the + array union operator to move an item with a certain key at the first position of an array:

    $item = $arr[$key];
    unset($arr[$key]);
    $arr = array($key => $item) + $arr;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题