如何重新排序从阵列中的数据? [英] How to reorder the data from array?

查看:111
本文介绍了如何重新排序从阵列中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只想问,如果我准备怎么重新安排使用站点名称的数组中的数据?我希望在站点名称是循环一次只显示一个数据。

just want to ask if how i gonna reorder the data from the array using site name? I want to display only one data at a time when the site name is loop.

例如网站顺序是这样

site1
site2
site3
site4

我想这就是它必须是为了通过率最高或编号:

What I want is this and it it must be order by highest rate or number:

name: John Doe
site: site1
rate: 10



name: Joen Doe
site: site2
rate: 10

name: Ejon Odeo
site: Site3
rate: 9.7

name: Ejosan Odeo
site: Site4
rate: 9.5

name: John Smith
site: site1
rate: 9.3

name: Jodsen Dsoe
site: site2
rate: 8

name: Efsdjon Oddeo
site: Site3
rate: 6.3

等等...

据中循环,直到将所有的打印。

It will loop until it will all print.

命名阵列:

Array ( [0] => Array ( [name] => name here [rate] => 10 [site] => site here)
Array ( [1] => Array ( [name] => name here [rate] => 9 [site] => site here)
Array ( [2] => Array ( [name] => name here [rate] => 8 [site] => site here)
Array ( [3] => Array ( [name] => name here [rate] => 7 [site] => site here)
Array ( [4] => Array ( [name] => name here [rate] => 6 [site] => site here)
Array ( [5] => Array ( [name] => name here [rate] => 5 [site] => site here)
Array ( [6] => Array ( [name] => name here [rate] => 5 [site] => site here)

网站阵
我想从阵列的每个站点名称显示只有一个人,当到达入端上的位置数组那么它将再次启动 EXS 网站名称,直到人所有信息都显示阵列。

Site Array I want to display only one person in every site name from array and when the site array is reached into the end then it will start again in exs site name until the persons array is all displayed.

Array ( [0] => Array ( [acronym] => exs [site_order] => 1 ) [1] => Array ( [acronym] => ts [site_order] => 1 ) [2] => Array ( [acronym] => ih [site_order] => 2 ) [3] => Array ( [acronym] => tp [site_order] => 3 ) [4] => Array ( [acronym] => tc [site_order] => 4 ) [5] => Array ( [acronym] => cfnm [site_order] => 5 ) [6] => Array ( [acronym] => sn [site_order] => 6 ) [7] => Array ( [acronym] => tla [site_order] => 7 ) [8] => Array ( [acronym] => bsc [site_order] => 8 ) [9] => Array ( [acronym] => tdp [site_order] => 9 ) [10] => Array ( [acronym] => lhd [site_order] => 10 ) [11] => Array ( [acronym] => ss [site_order] => 11 ) [12] => Array ( [acronym] => pov [site_order] => 12 ) [13] => Array ( [acronym] => rat [site_order] => 13 ) [14] => Array ( [acronym] => trw [site_order] => 14 ) [15] => Array ( [acronym] => tgs [site_order] => 15 ) [16] => Array ( [acronym] => tlm [site_order] => 16 ) [17] => Array ( [acronym] => ol [site_order] => 17 ) [18] => Array ( [acronym] => tb [site_order] => 18 ) [19] => Array ( [acronym] => ta [site_order] => 19 ) [20] => Array ( [acronym] => hfy [site_order] => 20 ) [21] => Array ( [acronym] => sd [site_order] => 21 ) [22] => Array ( [acronym] => si [site_order] => 22 ) [23] => Array ( [acronym] => tse [site_order] => 23 ) ) 

在网站阵列的缩写,是网站的名称。请帮我家伙

the acronym in site array is the site name. pls help me guys

推荐答案

看看 usort http://php.net/manual/en/function.usort.php

<?php

// your example array
$data = array(
    0 => array('name' => 'name 1', 'rate' => 1, 'site' => 'site 8'),
    1 => array('name' => 'name 2', 'rate' => 2, 'site' => 'site 9'),
    2 => array('name' => 'name 3', 'rate' => 4, 'site' => 'site 10'),
    3 => array('name' => 'name 4', 'rate' => 3, 'site' => 'site 11'),
    4 => array('name' => 'name 5', 'rate' => 6, 'site' => 'site 12'),
    5 => array('name' => 'name 6', 'rate' => 7, 'site' => 'site 13'),
    6 => array('name' => 'name 7', 'rate' => 5, 'site' => 'site 14')
);

// sort array with user defined function, see below
usort($data, 'sortByRate');

// now you can walk through the array and print it
foreach ($data as $item) {
    echo 'name: '.$item['name'].'<br>';
    echo 'site: '.$item['site'].'<br>';
    echo 'rate: '.$item['rate'].'<br>';
    echo '<br>';
}


// function wich sorts the array
function sortByRate($a, $b) {
    if ($a['rate'] == $b['rate']) {
        return 0;
    }
    return ($a['rate'] > $b['rate']) ? -1 : 1;
}

这篇关于如何重新排序从阵列中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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