在php中使用多个条件对关联数组进行排序

发布于 2024-09-16 07:51:23 字数 1228 浏览 9 评论 0原文

考虑下面的数组

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n')
    );

这里我想用 head 和 membkey 进行排序。相同 membkey 元素的顶部元素应该有 'head=y' 并回显为,

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n')
    );

我尝试如下

    function orderbymemberKey( $a, $b ){
    if ( $a[membkey] == $b[membkey] )
        return 0;
    return($a[membkey] < $b[membkey] )? -1 :1;
 }
 usort( $details, orderbymemberKey );
and it successfully order by membkey.

请提出任何建议。

Consider following array

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n')
    );

Here I would like to sort with head and membkey. Top element of same membkey element should have 'head=y' and echoed as,

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n')
    );

I tried it as follows

    function orderbymemberKey( $a, $b ){
    if ( $a[membkey] == $b[membkey] )
        return 0;
    return($a[membkey] < $b[membkey] )? -1 :1;
 }
 usort( $details, orderbymemberKey );
and it successfully order by membkey.

Any suggestions please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-09-23 07:51:23

你已经成功了一半(尽管你根据你的示例对 membkey 进行了向后排序):

function order_by_member_key($a, $b)
{
  if ($a['membkey'] == $b['membkey'])
  {
    // membkey is the same, sort by head
    if ($a['head'] == $b['head']) return 0;
    return $a['head'] == 'y' ? -1 : 1;
  }

  // sort the higher membkey first:
  return $a['membkey'] < $b['membkey'] ? 1 : -1;
}
usort($details, "order_by_member_key");

You're half way there (though you were sorting backwards for membkey based on your example):

function order_by_member_key($a, $b)
{
  if ($a['membkey'] == $b['membkey'])
  {
    // membkey is the same, sort by head
    if ($a['head'] == $b['head']) return 0;
    return $a['head'] == 'y' ? -1 : 1;
  }

  // sort the higher membkey first:
  return $a['membkey'] < $b['membkey'] ? 1 : -1;
}
usort($details, "order_by_member_key");
饮湿 2024-09-23 07:51:23

这个数组是从数据库中提取的吗?因为,如果是这样,您应该能够使用 ORDER BY 子句在 php 之外清理它。

Is this array being pulled from a database? Because, if so, you should be able to make use of ORDER BY clauses to clean it up outside of php.

罗罗贝儿 2024-09-23 07:51:23
<?php

$membkey = array();
$head = array();
foreach ($details as $key => $row) {
    $membkey[$key]  = $row['membkey'];
    $head[$key] = $row['head'];
}

array_multisort($membkey, SORT_DESC, $head, SORT_DESC, $details);

print_r($details);

或者,一个更通用的解决方案:

function sort_by($array) {
    $arguments = func_get_args();
    $array = array_pop($arguments);
    $variables = array();
    foreach ($arguments as $index => $key) {
        $variables[] = '$arguments['.$index.']';
        if ($index % 2 == 0) {
            $arguments[$index] = array();
            foreach ($array as $row) $arguments[$index][] = $row[$key];
        }
    }
    // call_user_func_array will not work in this case
    eval('array_multisort('.implode(', ', $variables).', $array);');
    return $array;
}

print_r(sort_by('membkey', SORT_DESC, 'head', SORT_DESC, $details));
<?php

$membkey = array();
$head = array();
foreach ($details as $key => $row) {
    $membkey[$key]  = $row['membkey'];
    $head[$key] = $row['head'];
}

array_multisort($membkey, SORT_DESC, $head, SORT_DESC, $details);

print_r($details);

Or, an even more generic solution:

function sort_by($array) {
    $arguments = func_get_args();
    $array = array_pop($arguments);
    $variables = array();
    foreach ($arguments as $index => $key) {
        $variables[] = '$arguments['.$index.']';
        if ($index % 2 == 0) {
            $arguments[$index] = array();
            foreach ($array as $row) $arguments[$index][] = $row[$key];
        }
    }
    // call_user_func_array will not work in this case
    eval('array_multisort('.implode(', ', $variables).', $array);');
    return $array;
}

print_r(sort_by('membkey', SORT_DESC, 'head', SORT_DESC, $details));
梦幻的心爱 2024-09-23 07:51:23

丑陋但有人在 php.net 上写了一个函数:
http://php.net/manual/en/function.sort.php

<?php

$array[0]['name']  = 'Chris';
$array[0]['phone'] = '3971095';
$array[0]['year']  = '1978';
$array[0]['address'] = 'Street 1';

$array[1]['name']  = 'Breanne';
$array[1]['phone'] = '3766350';
$array[1]['year']  = '1990';
$array[1]['address'] = 'Street 2';

$array[2]['name']  = 'Dusty';
$array[2]['phone'] = '1541120';
$array[2]['year']  = '1982';
$array[2]['address'] = 'Street 3';

function multisort($array, $sort_by, $key1, $key2=NULL, $key3=NULL, $key4=NULL, $key5=NULL, $key6=NULL){
    // sort by ?
    foreach ($array as $pos =>  $val)
        $tmp_array[$pos] = $val[$sort_by];
    asort($tmp_array);

    // display however you want
    foreach ($tmp_array as $pos =>  $val){
        $return_array[$pos][$sort_by] = $array[$pos][$sort_by];
        $return_array[$pos][$key1] = $array[$pos][$key1];
        if (isset($key2)){
            $return_array[$pos][$key2] = $array[$pos][$key2];
            }
        if (isset($key3)){
            $return_array[$pos][$key3] = $array[$pos][$key3];
            }
        if (isset($key4)){
            $return_array[$pos][$key4] = $array[$pos][$key4];
            }
        if (isset($key5)){
            $return_array[$pos][$key5] = $array[$pos][$key5];
            }
        if (isset($key6)){
            $return_array[$pos][$key6] = $array[$pos][$key6];
            }
        }
    return $return_array;
    }

//usage (only enter the keys you want sorted):

$sorted = multisort($array,'year','name','phone','address');
print_r($sorted);

//output:
Array ( [0] => Array ( [year] => 1978 [name] => Chris [phone] => 3971095 [address] => Street 1 ) [2] => Array ( [year] => 1982 [name] => Dusty [phone] => 1541120 [address] => Street 3 ) [1] => Array ( [year] => 1990 [name] => Breanne [phone] => 3766350 [address] => Street 2 ) )

Ugly but someone wrote a function on php.net:
http://php.net/manual/en/function.sort.php

<?php

$array[0]['name']  = 'Chris';
$array[0]['phone'] = '3971095';
$array[0]['year']  = '1978';
$array[0]['address'] = 'Street 1';

$array[1]['name']  = 'Breanne';
$array[1]['phone'] = '3766350';
$array[1]['year']  = '1990';
$array[1]['address'] = 'Street 2';

$array[2]['name']  = 'Dusty';
$array[2]['phone'] = '1541120';
$array[2]['year']  = '1982';
$array[2]['address'] = 'Street 3';

function multisort($array, $sort_by, $key1, $key2=NULL, $key3=NULL, $key4=NULL, $key5=NULL, $key6=NULL){
    // sort by ?
    foreach ($array as $pos =>  $val)
        $tmp_array[$pos] = $val[$sort_by];
    asort($tmp_array);

    // display however you want
    foreach ($tmp_array as $pos =>  $val){
        $return_array[$pos][$sort_by] = $array[$pos][$sort_by];
        $return_array[$pos][$key1] = $array[$pos][$key1];
        if (isset($key2)){
            $return_array[$pos][$key2] = $array[$pos][$key2];
            }
        if (isset($key3)){
            $return_array[$pos][$key3] = $array[$pos][$key3];
            }
        if (isset($key4)){
            $return_array[$pos][$key4] = $array[$pos][$key4];
            }
        if (isset($key5)){
            $return_array[$pos][$key5] = $array[$pos][$key5];
            }
        if (isset($key6)){
            $return_array[$pos][$key6] = $array[$pos][$key6];
            }
        }
    return $return_array;
    }

//usage (only enter the keys you want sorted):

$sorted = multisort($array,'year','name','phone','address');
print_r($sorted);

//output:
Array ( [0] => Array ( [year] => 1978 [name] => Chris [phone] => 3971095 [address] => Street 1 ) [2] => Array ( [year] => 1982 [name] => Dusty [phone] => 1541120 [address] => Street 3 ) [1] => Array ( [year] => 1990 [name] => Breanne [phone] => 3766350 [address] => Street 2 ) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文