按键sortingPHPmultidimensional array?
我想sorting我的PHP哈希表基于一个特定的关键。 数据结构如下所示:
print_r($mydata); Array( [0] => Array ( [type] => suite [name] => A-Name ) [1] => Array ( [type] => suite [name] => C-Name ) [2] => Array ( [type] => suite [name] => B-Name ) )
我试过ksort , sort , usort,但似乎没有工作。 我试图根据名称键两级下来进行sorting。
这是我使用usort的尝试:
function cmp($a, $b) { return $b['name'] - $a['name']; } usort($mydata, "cmp");
有没有一个简单的方法来做到这一点,或者我需要编写一个自定义的sortingfunction?
思考,更有用和实用http://php.net/manual/en/function.sort.php
function array_sort($array, $on, $order=SORT_ASC){ $new_array = array(); $sortable_array = array(); if (count($array) > 0) { foreach ($array as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { if ($k2 == $on) { $sortable_array[$k] = $v2; } } } else { $sortable_array[$k] = $v; } } switch ($order) { case SORT_ASC: asort($sortable_array); break; case SORT_DESC: arsort($sortable_array); break; } foreach ($sortable_array as $k => $v) { $new_array[$k] = $array[$k]; } } return $new_array; }
如何使用
$list = array( array( 'type' => 'suite', 'name'=>'A-Name'), array( 'type' => 'suite', 'name'=>'C-Name'), array( 'type' => 'suite', 'name'=>'B-Name') ); $list = array_sort($list, 'name', SORT_ASC); array(3) { [0]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "A-Name" } [2]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "B-Name" } [1]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "C-Name" } }
试试这个usortfunction:
function cmp($a, $b){ if ($a == $b) return 0; return ($a['name'] < $b['name']) ? -1 : 1; } $my_array = array( 0 => array ( 'type' => 'suite' ,'name' => 'A-Name' ) ,1 => array ( 'type' => 'suite' ,'name' => 'C-Name' ) ,2 => array ( 'type' => 'suite' ,'name' => 'B-Name' ) ); usort($my_array, "cmp");
如果您在类中使用它,则第二个参数将变为如下所示的数组:
usort($my_array, array($this,'cmp'));
array_multisort() – 对多维或multidimensional array进行sorting
<?php $a=array( array('a'=>5,'b'=>7),array('c'=>4,'d'=>2),array('e'=>0,'f'=>12) ); function cmp_sort($x,$y){ //your function to compare two keys if($x===$y) return 0; else return ($x<$y?1:-1); } uasort($a,'cmp_sort'); //call user-defined compare function print_r($a); //printing the sorted array ?>
输出=>
Array([2] => Array([e] => 0 [f] => 12)[1] => Array([c] => 4 [d] => 2) a] => 5 [b] => 7))
$this->aasort($array,"key"); function aasort (&$array, $key) { $sorter=array(); $ret=array(); reset($array); foreach ($array as $ii => $va) { $sorter[]=$va[$key]; } arsort($sorter); foreach ($sorter as $ii => $va) { $ret[]=$array[$ii]; } return $array=$ret; }
试试这个recursion模式,我在Magento REST API中使用:
$parameters = ['searchCriteria' => ['currentPage' => 1, 'pageSize' => 20]]; $parameters['searchCriteria']['filter_groups'][0]['filters'][0] = ['condition_type' => 'from', 'field' => 'updated_at', 'value' => '2017-01-01T00:00:00']; $parameters['searchCriteria']['filter_groups'][1]['filters'][0] = ['condition_type' => 'to', 'field' => 'updated_at', 'value' => '2017-12-31T23:59:59']; function sortArrayByKeyAsc($_params){ if(is_array($_params)){ uksort($_params, 'strnatcmp'); foreach ($_params as $key => $value){ if(is_array($value)){ $_params[$key] = sortArrayByKeyAsc($value); } } } return $_params; }