Rumah > pembangunan bahagian belakang > tutorial php > 二维数组按值排序

二维数组按值排序

WBOY
Lepaskan: 2016-06-06 20:11:20
asal
1228 orang telah melayarinya

<code>//排序前
$arr = array(
    0 => array('id' => 'C160001-12'),
    1 => array('id' => 'C160001-10'),
    2 => array('id' => 'C160001-11'),
    3 => array('id' => 'C160001-4'),
    4 => array('id' => 'C160001-6'),
    5 => array('id' => 'C160001-3'),
    6 => array('id' => 'C160001-2'),
    7 => array('id' => 'C160001-5'),
    8 => array('id' => 'C160001-0'),
    9 => array('id' => 'C160001-8'),
    10 => array('id' => 'C160001-1'),
    11 => array('id' => 'C160001-9'),
    12 => array('id' => 'C160001-7'),
    );</code>
Salin selepas log masuk
Salin selepas log masuk
<code>//排序后
$arr = array(
    0 => array('id' => 'C160001-0'),
    1 => array('id' => 'C160001-1'),
    2 => array('id' => 'C160001-2'),
    3 => array('id' => 'C160001-3'),
    4 => array('id' => 'C160001-4'),
    5 => array('id' => 'C160001-5'),
    6 => array('id' => 'C160001-6'),
    7 => array('id' => 'C160001-7'),
    8 => array('id' => 'C160001-8'),
    9 => array('id' => 'C160001-9'),
    10 => array('id' => 'C160001-10'),
    11 => array('id' => 'C160001-11'),
    12 => array('id' => 'C160001-12'),
    );</code>
Salin selepas log masuk
Salin selepas log masuk

求大神解答排序算法!
---------------------------------------华丽的分割线------------------------------------------
虽然楼下给出了答案,但是本人的另一位朋友又给出了一种更简便直观的方法,如下:

<code>usort($statementsample,function($a,$b){
    return (int)substr($a['id'],8)>(int)substr($b['id'],8);
            });</code>
Salin selepas log masuk
Salin selepas log masuk

回复内容:

<code>//排序前
$arr = array(
    0 => array('id' => 'C160001-12'),
    1 => array('id' => 'C160001-10'),
    2 => array('id' => 'C160001-11'),
    3 => array('id' => 'C160001-4'),
    4 => array('id' => 'C160001-6'),
    5 => array('id' => 'C160001-3'),
    6 => array('id' => 'C160001-2'),
    7 => array('id' => 'C160001-5'),
    8 => array('id' => 'C160001-0'),
    9 => array('id' => 'C160001-8'),
    10 => array('id' => 'C160001-1'),
    11 => array('id' => 'C160001-9'),
    12 => array('id' => 'C160001-7'),
    );</code>
Salin selepas log masuk
Salin selepas log masuk
<code>//排序后
$arr = array(
    0 => array('id' => 'C160001-0'),
    1 => array('id' => 'C160001-1'),
    2 => array('id' => 'C160001-2'),
    3 => array('id' => 'C160001-3'),
    4 => array('id' => 'C160001-4'),
    5 => array('id' => 'C160001-5'),
    6 => array('id' => 'C160001-6'),
    7 => array('id' => 'C160001-7'),
    8 => array('id' => 'C160001-8'),
    9 => array('id' => 'C160001-9'),
    10 => array('id' => 'C160001-10'),
    11 => array('id' => 'C160001-11'),
    12 => array('id' => 'C160001-12'),
    );</code>
Salin selepas log masuk
Salin selepas log masuk

求大神解答排序算法!
---------------------------------------华丽的分割线------------------------------------------
虽然楼下给出了答案,但是本人的另一位朋友又给出了一种更简便直观的方法,如下:

<code>usort($statementsample,function($a,$b){
    return (int)substr($a['id'],8)>(int)substr($b['id'],8);
            });</code>
Salin selepas log masuk
Salin selepas log masuk

<code>function my_sort($a,$b)
{
    $a = explode('-',$a['id'])[1];
    $b = explode('-',$b['id'])[1];
    if ($a==$b) return 0;
    return ($a</code>
Salin selepas log masuk

使用usort 方法比较灵活。

array_multisort() 函数 示例如下

<code>$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

$type = array();
foreach ($data as $key => $value) {
    $type[] = $value['edition'];
}
array_multisort($type, SORT_ASC, $data);
var_dump($data);</code>
Salin selepas log masuk
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan