douchensou6495 2014-07-11 08:23
浏览 35
已采纳

PHP:关联数组交换使用第一个元素搜索值并对其余元素进行排序

I receive the following array (key is the value of the id element) and need to sort it, but actually no PHP sortfunction can help me here. I get, out of a session, a value which is equal to one of the id values. For example I get the Value 2. And I have to swap the array, which has the id element equals the session value (here: 2) with the first element of my array. The other elements I want to sort ascending in aspect to the key of my main array.

Heres the array:

Array
(
[1] => Array
    (
        [id] => 1
        [name] => dummy11
    )

[2] => Array
    (
        [id] => 2
        [name] => dummy22
    )

[3] => Array
    (
        [id] => 3
        [name] => dummy33
    )

[4] => Array
    (
        [id] => 4
        [name] => dummy44
    )

[5] => Array
    (
        [id] => 5
        [name] => dummy55
    )

[6] => Array
    (
        [id] => 6
        [name] => dummy66
    )

[7] => Array
    (
        [id] => 7
        [name] => dummy77
    )

[8] => Array
    (
        [id] => 8
        [name] => dummy88
    )

[9] => Array
    (
        [id] => 9
        [name] => dummy99
    )

[10] => Array
    (
        [id] => 10
        [name] => dummy10
    )
)

My Problem is now to sort it that way, that I receive the following array:

Array
(
[2] => Array
    (
        [id] => 2
        [name] => dummy22
    )
[1] => Array
    (
        [id] => 1
        [name] => dummy11
    )

[3] => Array
    (
        [id] => 3
        [name] => dummy33
    )

[4] => Array
    (
        [id] => 4
        [name] => dummy44
    )

[5] => Array
    (
        [id] => 5
        [name] => dummy55
    )

[6] => Array
    (
        [id] => 6
        [name] => dummy66
    )

[7] => Array
    (
        [id] => 7
        [name] => dummy77
    )

[8] => Array
    (
        [id] => 8
        [name] => dummy88
    )

[9] => Array
    (
        [id] => 9
        [name] => dummy99
    )

[10] => Array
    (
        [id] => 10
        [name] => dummy10
    )
)

If someone could help me I'd be grateful.

  • 写回答

1条回答 默认 最新

  • douxiapi4381 2014-07-11 08:51
    关注

    You can create a custom sort using uksort() (The u is for user defined, k is sort by key). This can then sort but check for special values:

    uksort($yourArray, function($a, $b){
        if($a == $SESSION['someId']){//Or '2' or whatever value you want to check.
            return -1;
        }elseif($b == $SESSION['someId']){
            return 1;
        }
        return $a - $b;
    });
    

    Alternatively you can simply sort by key and use an answer from this question to move your item to the top:

    ksort($yourArray);
    $key          = $SESSION['someId'];//Or '2' or whatever value you want to check.
    $last_value   = array_pop($yourArray);
    $yourArray    = array_merge(array($key => $last_value), $yourArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效