How to sort multiple arrays in PHP

余生长醉 提交于 2019-12-07 03:18:15

问题


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] => 
    )

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


回答1:


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.

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);


?>

Here goes a working demo.




回答2:


How about this simpler one

$arr = array("k"=>array("A","B","C"),"l"=>array(15,6,15),"n"=>array("k","l","n"));
array_multisort($arr["k"],SORT_NUMERIC,SORT_DESC,$arr["l"],$arr["n"]);
var_dump($arr);



回答3:


Doesn't it work to just rsort the score array?

rsort($data['score'], SORT_NUMERIC);



回答4:


I have managed to do this, i was just after a more efficient way;

$array = array(
            '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 => '' )
);

arsort($array['score'], SORT_NUMERIC );
foreach($array['score'] as $key => $val ) {
    $newarray['display_name'][] = $array['display_name'][$key];
    $newarray['proffesion'][] = $array['proffesion'][$key];
    $newarray['score'][] = $array['score'][$key];
    $newarray['img'][] = $array['img'][$key];
}

print_r($newarray);

returns

Array
(
    [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] => 
        )

)



回答5:


Use rsort()

<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
reset($fruits);
while (list($key, $val) = each($fruits)) {
    echo "$key = $val\n";
}
?>  

This example would display:

0 = orange
1 = lemon
2 = banana
3 = apple



回答6:


The most elegant solution that I could find would not reorder the data structure but merely access it in a different fashion.

$scores = $data['score'];
arsort($scores);
$keys_ordered_by_score = array_keys($scores);

Now you can, say, grab the display_name and "proffesion" that has the highest score by the following:

$first_place = $keys_ordered_by_score[0];
echo $data['display_name'][$first_place],
     ' is a ', $data['proffesion'][$first_place];

Of course, if you really need to reorder the data structure, this idea is useless to you. Any of the other answers using array_multisort() will likely suit that need.




回答7:


This will not re-sort them for you, but it will let you go through them in the order you want. You can reassign them if you want, but I'd just use this for the output order.

Edit: This will not work, due to the possibility of non-unique key values. See Comments below, and learn from my mistake

$sort_order = $array['score'];
arsort($sort_order);
$sort_order = array_flip($sort_order);
foreach($sort_order as $key){
    echo $array['display_name'][$key].' - '.$array['score'][$key];
}


来源:https://stackoverflow.com/questions/2696610/how-to-sort-multiple-arrays-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!