[编码惹祸]PHP  array_multisort() 排序理解和自己遇到的编码和如何处理问题备份。

jackxiang 2009-4-15 18:38 | |
我们需要对这个数组先按照score【分数排序:由高到低】,然后再由name[姓名低到高排序]:
特别注意:array_multisort对姓名排序时候要utf8【假如是UTF8】转为gbk,否则排序出现错误,切忌切忌!!!
1.排序代码:[特别注意:我在排序分数的时候有个千分位输出,最后排序发现它会当成整形去处理,逗号{千分位分隔符}会截断],所以,最好是先排好【对score排好--》在通过number_format来千分位输出,见下代码:】
由于调用用户姓名接口是utf8输出,我们这儿的php文件是gbk编码【由于大部规定:除了html模板,接口返回编码和数据存储为utf8外其余都是gbk编码,因此:我这个modle层的编码也是gbk的】,为此,必须转为gbk来进行中文名的二次排序【首先是按照分数来排序,这个数组在后面给出】,
注意:转为GBK编码片段: iconv("UTF-8","GBK",$v);
foreach($resultname as $k =>$v)
      {
              $arraydisplay[$i][name] = iconv("UTF-8","GBK",$v);
              $arraydisplay[$i][uid] = $k;
          $arraydisplay[$i][score] =  $json2array[$k];
          $arraydisplay[$i][potourl] = $resulticon[$k];
          foreach ($level_type as $kka =>$vvc)
          {
            if(($json2array[$k]<=$vvc[1])&&($json2array[$k]>=$vvc[0]))
            {
              $imgurl = $LeavelImg[$kka][0];//取得图片地址  
              $loopimg = $LeavelImg[$kka][1];
              for($jj=0;$jj<$loopimg;$jj++)
              {              
                $imgnum[] = $imgurl;
              }
              break;    
            }
          }
          $arraydisplay[$i][imgsrc] = $imgnum;
          unset($imgnum);
            
              $i++;
      }



排序代码:
      $arraydisplay = array_chunk($arraydisplay,10,TRUE);//截取前10个人的数据给smarty          $data = $arraydisplay[0];
      unset($arraydisplay);  
      unset($arraydisplay);  
      foreach ($data as $key => $row)
      {  
                      $score[$key]  = $row['score'];
                      $name[$key] = $row['name'];
      
      }  
      array_multisort($score, SORT_DESC,  $name, SORT_ASC, $data);
      for($iii=0;$iii<10;$iii++)
      {
        $data[$iii]['name'] = iconv("GBK","UTF-8",$data[$iii]['name']);  
        $data[$iii]['score'] =  number_format($data[$iii]['score']);
      }
      $result = $data;
    }






Array
(
    [0] => Array
        (
            [name] => 盛大师
            [uid] => 1159375663
            [score] => 88,897,776
            [potourl] => http://p8.sinaimg.cn/1159375663/50/1236592606
            [imgsrc] => Array
                (
                    [0] => renyuan_level_1.gif
                    [1] => renyuan_level_1.gif
                    [2] => renyuan_level_1.gif
                    [3] => renyuan_level_1.gif
                    [4] => renyuan_level_1.gif
                    [5] => renyuan_level_1.gif
                )

        )

    [1] => Array
        (
            [name] => 你好
            [uid] => 1377417885
            [score] => 728,888
            [potourl] => http://p6.sinaimg.cn/1377417885/50/1234321817
            [imgsrc] => Array
                (
                    [0] => renyuan_level_1.gif
                    [1] => renyuan_level_1.gif
                    [2] => renyuan_level_1.gif
                    [3] => renyuan_level_1.gif
                )

        )

    [2] => Array
        (
            [name] => 笨蛋
            [uid] => 1584778420
            [score] => 77,909
            [potourl] => http://p5.sinaimg.cn/1584778420/50/
            [imgsrc] => Array
                (
                    [0] => renyuan_level_2.gif
                    [1] => renyuan_level_2.gif
                    [2] => renyuan_level_2.gif
                    [3] => renyuan_level_2.gif
                    [4] => renyuan_level_2.gif
                )

        )

    [3] => Array
        (
            [name] => 吥喜欢
            [uid] => 1581862283
            [score] => 17,776
            [potourl] => http://p4.sinaimg.cn/1581862283/50/1239087886
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                    [5] => renyuan_level_3.gif
                )

        )

    [4] => Array
        (
            [name] => 阿里爸
            [uid] => 1582003980
            [score] => 17,776
            [potourl] => http://p5.sinaimg.cn/1582003980/50/1232551415
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                    [5] => renyuan_level_3.gif
                )

        )

    [5] => Array
        (
            [name] => 科比
            [uid] => 1582157323
            [score] => 17,776
            [potourl] => http://p4.sinaimg.cn/1582157323/50/1232615329
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                    [5] => renyuan_level_3.gif
                )

        )

    [6] => Array
        (
            [name] => 琳琳琳
            [uid] => 1281167662
            [score] => 17,776
            [potourl] => http://p7.sinaimg.cn/1281167662/50/1239361854
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                    [5] => renyuan_level_3.gif
                )

        )

    [7] => Array
        (
            [name] => 克里斯大王
            [uid] => 1141457724
            [score] => 8,988
            [potourl] => http://p5.sinaimg.cn/1141457724/50/1232521326
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                )

        )

    [8] => Array
        (
            [name] => 苗艳宏
            [uid] => 1581837512
            [score] => 8,988
            [potourl] => http://p1.sinaimg.cn/1581837512/50/1236940821
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                )

        )

    [9] => Array
        (
            [name] => 朱阿里
            [uid] => 1180166542
            [score] => 8,988
            [potourl] => http://p7.sinaimg.cn/1180166542/50/
            [imgsrc] => Array
                (
                    [0] => renyuan_level_3.gif
                    [1] => renyuan_level_3.gif
                    [2] => renyuan_level_3.gif
                    [3] => renyuan_level_3.gif
                    [4] => renyuan_level_3.gif
                )

        )

)

          



对二维数组里的某列键值进行排序有点相当于sql语句,array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。 关联(string)键名保持不变,但数字键名会被重新索引。 :
参考自己http://www.jb51.net/article/25882.htm


输出如下所示:
---------- 调试PHP ----------

Array
(
    [0] => Array
        (
            [id] => 6
            [value] => 6-1
            [parent] => 3
        )

    [1] => Array
        (
            [id] => 5
            [value] => 5-1
            [parent] => 2
        )

    [2] => Array
        (
            [id] => 4
            [value] => 4-1
            [parent] => 2
        )

    [3] => Array
        (
            [id] => 3
            [value] => 3-1
            [parent] => 1
        )

    [4] => Array
        (
            [id] => 2
            [value] => 2-1
            [parent] => 1
        )

    [5] => Array
        (
            [id] => 1
            [value] => 1-1
            [parent] => 1
        )

)

输出完成 (耗时 0 秒) - 正常终止

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1715/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2014-5-14 14:53
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]