php - 如何对这样的数组进行排序 求教

查看:87
本文介绍了php - 如何对这样的数组进行排序 求教的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

**

按照content2排序

**

array(5) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["content2"]=>
    string(2) "XL"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["content2"]=>
    string(1) "L"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(1) "3"
    ["content2"]=>
    string(3) "XXL"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(1) "4"
    ["content2"]=>
    string(1) "L"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(1) "5"
    ["content2"]=>
    string(2) "XL"
  }
}

解决方案

PHP多维数组排序array

/**
* Sort array by filed and type, common utility method.
* @param array $data
* @param string $sort_filed
* @param string $sort_type SORT_ASC or SORT_DESC
*/
public function sortByOneField($data, $filed, $type)
{
    if (count($data) <= 0) {
        return $data;
    }
    foreach ($data as $key => $value) {
        $temp[$key] = $value[$filed];
    }
    array_multisort($temp, $type, $data);
    return $data;
}

这篇关于php - 如何对这样的数组进行排序 求教的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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