如何在PHP中对多个数组进行排序 [英] How to sort multiple arrays in PHP

查看:159
本文介绍了如何在PHP中对多个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本来生成数据数组,但现在想按分数顺序显示.数组输出如下;

i have wrote a script to produce an array of data but now want to display in order of score. The array outputs as follows;

[display_name] => Array
    (
        [0] => ACT_Web_Designs
        [1] => user1_design
        [2] => user2_design
    )

[proffesion] => Array
    (
        [0] => Web Developer
        [1] => web developer
        [2] => Web Developer
    )

[score] => Array
    (
        [0] => 15
        [1] => 6
        [2] => 15
    )

[img] => Array
    (
        [0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
        [1] => 
        [2] => 
    )

因此,简而言之,我希望将其转换为以下内容;

so in a nutshell I am wanting it to be converted as follows;

    [display_name] => Array
    (
        [0] => ACT_Web_Designs
        [1] => user2_design
        [2] => user1_design
    )

[proffesion] => Array
    (
        [0] => Web Developer
        [1] => web developer
        [2] => Web Developer
    )

[score] => Array
    (
        [0] => 15
        [1] => 15
        [2] => 6
    )

[img] => Array
    (
        [0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
        [1] => 
        [2] => 
    )

我一直在查看asort(),但无法进行任何操作.任何帮助将不胜感激.

I have been looking at asort() but cant get anything to work. any help would be much appreciated.

推荐答案

这正是PHP

This is exactly where the PHP's array_multisort comes to use. It is a case where you want to sort many arrays based on the comparison happening in just one of them.

我将数组score修改为具有不同的值.

I've modified the array score to have distinct values.

<?php

$arr = array(
                'display_name' => array('ACT_Web_Designs','user1_design','user2_design'),
                'proffesion' => array('Web Developer','web developer','web developer'),
                'score' => array(12,6,15),
                'img' => array('./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic','','')
            );

var_dump($arr);
array_multisort($arr['score'], SORT_ASC, SORT_NUMERIC,
                $arr['display_name'],
                $arr['proffesion'],
                $arr['img']
                );
var_dump($arr);


?>

此处是一个有效的演示.

这篇关于如何在PHP中对多个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆