doulu4233 2016-11-06 12:30
浏览 48
已采纳

PHP - 如何按值对2个变量进行排序?

I wanna sort this 2 variables by the variable 'wons'

I mean, I first wanna sort the variable 'wons' and I want the respective 'final' like:

$final[0] = 'pinco'; 
$final[1] = 'pallino';

$wons[0] = 3; 
$wons[1] = 7;

Now i want to sort this 2 variables for 'wons' variable like I want this result:

I want the first result to be pallino and the score 7 and etc..

Can anyone help me? I have tried so hard.. The language is PHP

  • 写回答

2条回答 默认 最新

  • duandan4680 2016-11-06 12:58
    关注

    The function you are looking for is called array_multisort

    See: http://php.net/manual/en/function.array-multisort.php

    The first example explains the same situation you are describing:

    Example #1 Sorting multiple arrays

    <?php
    $ar1 = array(10, 100, 100, 0);
    $ar2 = array(1, 3, 2, 4);
    array_multisort($ar1, $ar2);
    
    var_dump($ar1);
    var_dump($ar2);
    ?>
    

    In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

    array(4) {
      [0]=> int(0)
      [1]=> int(10)
      [2]=> int(100)
      [3]=> int(100)
    }
    array(4) {
      [0]=> int(4)
      [1]=> int(1)
      [2]=> int(2)
      [3]=> int(3)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了