doupai6875 2013-08-22 14:02
浏览 23
已采纳

排序阵列无法正常工作

The sorting of array in alphabetical order is not outputting correctly.

It outputted as:

Demo qwerty Demo3 Test1 Test2 New1

Instead of:

Demo Demo3 New1 qwerty Test1 Test2

Code:

<?php

$dbresults= array ( "0"  => array ( "id" => "1",
                              "cb_boutiquename1" => "Test1",
                              "cb_boutiquename2" => "Test2",
                              "cb_boutiquename3" => "New1"
                                 ),
              "1" => array ( "id" => "2",
                              "cb_boutiquename1" => "Demo",
                              "cb_boutiquename2" => "qwerty",
                              "cb_boutiquename3" => "Demo3"
                                 )
            );

    function sortarray($a, $b) {
        return strcmp($a["cb_boutiquename$i"], $b["cb_boutiquename$i"]);
    }

    usort($dbresults, "sortarray");

    while (list($key, $value) = each($dbresults)) {
        $results[] = $value ;
    }

    foreach($results as $result) {
        $i = 1;
        while (array_key_exists("cb_boutiquename$i", $result)) {
        if ($result["cb_boutiquename$i"] !='') {
            echo '<a href=' . cbSef( 'index.php?option=com_comprofiler&task=page&user=' . (int) $result['id'] . '&b=' . $i . getCBprofileItemid( false )) . '>' . $result["cb_boutiquename$i"] . '</a><br />';
        }
        ++$i;
        }
    }
?>
  • 写回答

2条回答 默认 最新

  • douqingzhi0980 2013-08-22 14:22
    关注

    Your problem is that in here:

    function sortarray($a, $b) {
        return strcmp($a["cb_boutiquename$i"], $b["cb_boutiquename$i"]);
    }
    

    $i is undefined so you are basically comparing one undefined array offset with another.

    If you want to specify which of the cb_boutiquename fields to specify at runtime then what you probably want to do is include the $i in the function, on php this can be done with a function that returns a closure (PHP 5.3+).

    function getSorter($idx){
           return function($a, $b) use ($idx) {
               return strcmp($a["cb_boutiquename$idx"], $b["cb_boutiquename$idx"]);
           };
    }
    

    This returns a function that closes over the supplied argument and is appropriate for use as a comparison function for usort. So for your sort call you would use:

    usort($dbresults, getSorter(1));
    

    Or for a more general solution that does not assume the cb_boutiquename prefix on the properties you can change this to

    function getSorter($idx){
           return function($a, $b) use ($idx) {
               return strcmp($a[$idx], $b[$idx]);
           };
    }
    usort($dbresults, getSorter("cb_boutiquename1"));
    

    This will allow you to sort any array of arrays by an one of the indexes.

    UPDATE I completely misunderstood the goal of this exercise. What you want to do is to flatten your array prior to doing any sorting.

        $dbresults= array ( "0"  => array ( "id" => "1",
                              "cb_boutiquename1" => "Test1",
                              "cb_boutiquename2" => "Test2",
                              "cb_boutiquename3" => "New1"
                                 ),
              "1" => array ( "id" => "2",
                              "cb_boutiquename1" => "Demo",
                              "cb_boutiquename2" => "qwerty",
                              "cb_boutiquename3" => "Demo3"
                                 )
            );
    
    // flatten out the array
    $results = array();
    foreach($dbresults as $k=>$v){
        foreach ($v as $key=>$value) {
              if (substr($key, 0,15) == "cb_boutiquename"){
                  $results[] = array("id"=>$v["id"], "cb_boutiquename"=>$value, "i"=>substr($key, 15));
              }
        }
    
    }
    
    usort($results, function($a, $b){ return strcasecmp($a["cb_boutiquename"], $b["cb_boutiquename"]); });
    foreach($results as $result){
        echo '<a href=' . cbSef( 'index.php?option=com_comprofiler&task=page&user=' . (int) $result['id'] . '&b=' . $result["i"] . getCBprofileItemid( false )). '>' . $result["cb_boutiquename"] . '</a><br />'."
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法