按特定值对多维数组进行排序(使用asort和resort)

时间:2012-04-19 20:31:59

标签: php

我意识到还有其他一些与此有关的问题 - 但要么是对我没有意义,要么就是我不需要的工作。

我有一个数组,可以使用asort()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上发布的一些示例,但我必须误解它们,因为它们无法正常工作。我怎么能做到这一点?

修改 Jonathan给出了字母排序的正确答案,略作修改:

//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');

3 个答案:

答案 0 :(得分: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

处理的代码

答案 1 :(得分: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/

答案 2 :(得分: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