0

我意识到还有其他几个与此有关的问题 - 但它们对我来说没有意义,或者不按我的需要工作。

我有一个数组,很容易通过使用asort()and来按字母顺序对第一个值进行排序arsort()。我的多维数组的一个例子可能是:

[{"Name":"Amber","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Bob","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Hans","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Jeff","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Michael","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0}]

使用asort()并按字母顺序或反向字母顺序排序arsort()"Name"现在我需要相同的功能,仅基于"dealType".

我已经尝试了一些已经在 Stackoverflow 上发布的示例,但我必须误解它们,因为它们不起作用。我怎么能做到这一点?

编辑 乔纳森给出了按字母排序的正确答案,稍作修改:

//the custom function to do our sort
function cmp($a,$b){
    //get which string is less or 0 if both are the same
    $cmp = strcasecmp($a->dealType, $b->dealType);
    //if the strings are the same, check name
    return $cmp;
}
//sort using a custom function
usort($obj, 'cmp');

下面的代码用于反向字母排序:

//the custom function to do our sort
function cmp($a,$b){
    //get which string is less or 0 if both are the same
    $cmp = strcasecmp($b->dealType, $a->dealType);
    //if the strings are the same, check name
    return $cmp;
}
//sort using a custom function
usort($obj, 'cmp');
4

3 回答 3

2

您应该使用usort这将允许您使用自定义比较函数来确定排序时哪个值首先出现。

就像是:

<?php
//the data you supplied. normally just an array
$data = array ( 0 => array ( 'Name' => 'Amber', 'date' => '', 'dealType' => 'deal1', 'id' => '***@***.com', 'registered' => 0, ), 1 => array ( 'Name' => 'Bob', 'date' => '', 'dealType' => 'deal5', 'id' => '***@***.com', 'registered' => 0, ), 2 => array ( 'Name' => 'Hans', 'date' => '', 'dealType' => 'deal3', 'id' => '***@***.com', 'registered' => 0, ), 3 => array ( 'Name' => 'Jeff', 'date' => '', 'dealType' => 'deal2', 'id' => '***@***.com', 'registered' => 0, ), 4 => array ( 'Name' => 'Michael', 'date' => '', 'dealType' => 'deal1', 'id' => '***@***.com', 'registered' => 0, ), );

//show what we got going into sort
echo '<pre>'.print_r($data, 1).'</pre>';

function cmp($a,$b){
    //get which string is less or 0 if both are the same
    $cmp = strcasecmp($a['dealType'], $b['dealType']);
    //if the strings are the same, check name
    if($cmp == 0){
        //compare the name
        $cmp = strcasecmp($a['Name'], $b['Name']);
    }
    return $cmp;
}
//sort using a custom function
usort($data, 'cmp');

//show what we got after sort
echo '<pre>'.print_r($data, 1).'</pre>';
?>

这将dealType首先排序,如果相同,则Name第二个dealType。如果要反向排序,则可以交换调用的顺序和$a调用顺序。$bstrcasecmp()

编辑:这些数据可能是从数据库中提取的。如果是这样,那么只是ORDER BY列。你可以这样做SELECT * FROM table ORDER BY dealType ASC,Name ASC

Edit2:更新了代码以不使用匿名函数。您还可以查看在ideone工作的代码

于 2012-04-19T20:35:55.953 回答
1

如果我正确理解了您的问题,则您有数组(例如 A)数组(例如 Bs),并且您想按其中的某个值对 Bs 进行排序。如果那是正确的,我之前也遇到过同样的问题,并制作了名为 line_sort 的自定义函数。如果需要,可以通过示例使用它:

$array=json_decode('[{"Name":"Amber","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Bob","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Hans","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Jeff","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Michael","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0}]');
$array=line_sort("dealType",$array);

第一个值是要排序的键,第二个是要排序的数组,这是函数代码(抱歉不是英文):

function line_sort($klic,$hodnoty)
{
  if (!is_array($hodnoty))
  {
    trigger_error("Second parameter has to be multidimensional array", E_USER_WARNING );
    return;
  }
  for ($x=0;$x<count($hodnoty)-1;$x++)
  {
    for ($y=count($hodnoty)-1;$y>=$x;$y--)
    {
      $radek1=$hodnoty[$y];
      $radek2=$hodnoty[$y+1];
      if ((isset($radek1[$klic]) && isset($radek2[$klic])) && (intval($radek1[$klic]) > intval($radek2[$klic])) || (isset($radek2[$klic]) && !isset($radek1[$klic])))
      {
        $hodnoty[$y]=$radek2;
        $hodnoty[$y+1]=$radek1;
        continue;
      }
      else if((isset($radek1[$klic]) && isset($radek2[$klic])) && (intval($radek1[$klic]) == intval($radek2[$klic])))
      {
        if (strcmp($radek1[$klic],$radek2[$klic])>0)
        {
          $hodnoty[$y]=$radek2;
          $hodnoty[$y+1]=$radek1;
        }
      }
    }
  }
  return $hodnoty;
}

如果您想查看此函数的输出示例,请查看:http: //test.hanzlsoft.eu/

于 2012-04-19T20:52:48.153 回答
0

我相信您正在寻找array_multisort(): http: //php.net/manual/en/function.array-multisort.php

例子:

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

来源: http: //php.net/manual/en/function.array-multisort.php#example-4558

于 2012-04-19T20:34:37.410 回答