Home > php教程 > php手册 > body text

PHP按指定键值对二维数组进行排序的方法,

WBOY
Release: 2016-06-13 08:49:09
Original
1531 people have browsed it

PHP按指定键值对二维数组进行排序的方法,

本文实例讲述了PHP按指定键值对二维数组进行排序的方法。分享给大家供大家参考,具体如下:

问题:

有数组:复制代码 代码如下:array(0=>array('id'=>1,'price'=>50),1=>array('id'=>2,'price'=>60));
要求根据数组的price这个字段进行排序。

实现代码如下:

<&#63;php 
$array[] = array('id'=>1,'price'=>50);
$array[] = array('id'=>2,'price'=>70);
$array[] = array('id'=>3,'price'=>30);
$array[] = array('id'=>4,'price'=>20);
foreach ($array as $key=>$value){
  $id[$key] = $value['id'];
  $price[$key] = $value['price'];
}
array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array);
echo '<pre class="brush:php;toolbar:false">';
print_r($array);
echo '
Copy after login
'; ?>

运行结果:

Array
(
[0] => Array
(
[id] => 2
[price] => 70
)
[1] => Array
(
[id] => 1
[price] => 50
)
[2] => Array
(
[id] => 3
[price] => 30
)
[3] => Array
(
[id] => 4
[price] => 20
)
)

Copy after login

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php删除数组中重复元素的方法
  • php提交post数组参数实例分析
  • php数组函数array_key_exists()小结
  • PHP多维数组遍历方法(2种实现方法)
  • PHP中对数组的一些常用的增、删、插操作函数总结
  • 详解PHP对数组的定义以及数组的创建方法
  • 基于php实现随机合并数组并排序(原排序)
  • php实现遍历多维数组的方法
  • PHP多维数组转一维数组的简单实现方法
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!