English 中文(简体)
具有特定价值(利用 as和ras)的多面阵列
原标题:Sort multidimensional array by a particular value (using asort and rasort)
  • 时间:2012-04-19 20:31:59
  •  标签:
  • php

我认识到,还有其他几个问题——但是,这些问题要么对我有意义,要么不做我所需要的工作。

我有一个阵列,通过使用<代码>sort(>和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}]

使用<代码>sort(和arsort(>>,按Name> 字母或正反字母顺序排列。 现在,我需要同样的功能,只能依据dealType>

I have tried a few examples already posted on Stackoverflow, but I must misunderstand them because they are not working. How might I accomplish this?

EDIT Jonathan gave the correct answer for alphabetical sorting, with a slight modification:

//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 );
最佳回答

页: 1 这将使你能够使用一种习俗比较功能,以确定在进行分类时首先会产生什么价值。

例如:

<?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> ;
?>

页: 1 类型首先,Name 其次,deal 类型相同。 如果你想进行反向,那么你可以在<代码>/code>和上打下<>tcasecmp(>。

Edit:Chances正在从一个数据库中提取这一数据。 如果是的话,则只有<条码>。 页: 1 页: 1 类型ASC,Name ASC

Edit2:更新该法典,使之不使用匿名功能。 也可查看

问题回答

If i understand your problem correctly, you have array (say A) of arrays (say Bs) and you want to sort Bs by some value in them. If thats right, i had the same problem before and made custom function called line_sort. If you want, you can use it by the example:

$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;
}

如果你想看到这一职能的产出实例,请查阅:





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签