Rumah > pembangunan bahagian belakang > tutorial php > 请问数组怎么多条件排序

请问数组怎么多条件排序

WBOY
Lepaskan: 2016-06-23 13:26:02
asal
1112 orang telah melayarinya

array(
       '000-00'=>array(
                                    'qty'=>20,
                                    'price'=>200
                                  ),
       '0001-00'=>array(
                                     'qty'=>10,
                                    'price'=>100
                                  ),
      '0002-00'=>array(
                                      'qty'=>30,
                                     'price'=>50
                                 )

 )

请问怎么样先按qty由小到大排序,然后按Price由大到小排序。


回复讨论(解决方案)

$a = array(       '000-00'=>array(                                    'qty'=>20,                                    'price'=>200                                  ),       '0001-00'=>array(                                     'qty'=>10,                                    'price'=>100                                  ),      '0002-00'=>array(                                      'qty'=>30,                                     'price'=>50                                 ) );foreach($a as $k=>$v) {  $qty[] = $v['qty'];  $price[] = $v['price'];}array_multisort($qty, $price, SORT_DESC, $a);print_r($a);
Salin selepas log masuk
Salin selepas log masuk
Array(    [0001-00] => Array        (            [qty] => 10            [price] => 100        )    [000-00] => Array        (            [qty] => 20            [price] => 200        )    [0002-00] => Array        (            [qty] => 30            [price] => 50        ))
Salin selepas log masuk
Salin selepas log masuk

$a = array(       '000-00'=>array(                                    'qty'=>20,                                    'price'=>200                                  ),       '0001-00'=>array(                                     'qty'=>10,                                    'price'=>100                                  ),      '0002-00'=>array(                                      'qty'=>30,                                     'price'=>50                                 ) );foreach($a as $k=>$v) {  $qty[] = $v['qty'];  $price[] = $v['price'];}array_multisort($qty, $price, SORT_DESC, $a);print_r($a);
Salin selepas log masuk
Salin selepas log masuk
Array(    [0001-00] => Array        (            [qty] => 10            [price] => 100        )    [000-00] => Array        (            [qty] => 20            [price] => 200        )    [0002-00] => Array        (            [qty] => 30            [price] => 50        ))
Salin selepas log masuk
Salin selepas log masuk

这个函数真方便!

请问子数组再多几个是不是也可以,多几个后好像不行了

array(
       '000-00'=>array(
                                    'qty'=>20,
                                    'price'=>200,
                                    ‘status’=>1,
                                    'lirun'=>0.21,
                                    'name'=>'sdff',
                                  ),
       '0001-00'=>array(
                                     'qty'=>10,
                                    'price'=>100,
                                     ‘status’=>1,
                                    'lirun'=>0.22,
                                    'name'=>'erw',
                                  ),
      '0002-00'=>array(
                                      'qty'=>30,
                                     'price'=>50,
                                      ‘status’=>1,
                                    'lirun'=>0.23,
                                    'name'=>'sdfd',
                                 )

 )

可以了,原来变量名要跟数组里面的名称要一样才行

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