在倒序PHP数组排序出来 [英] PHP array out sort in desc order

查看:177
本文介绍了在倒序PHP数组排序出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code这里:我尝试使用排序功能而不是成功。这是我的工作数组code:

我的code desplay输出没有排序顺序:

 < PHP 的foreach($ aCities为$关键=> $ VAL)
 {
     的foreach($磁盘空间[$ VAL]为$关键=> $ val2的)
     {
         如果(使用isset($ val2的['数据']))
         {
             //'兆字节%.F'= 155.000000%;
             preg_match(/'(.*?)ytes \\%F'= \\%(*);?/,$ val2的。['数据'],$ AVAL);
             回声($ VAL'.ceil($ AVAL [2])。''$ AVAL [1]'< BR />'。);
         }
     }
 }
?>

code的输出:

 第3 MB
Ť6 MB
我10 MB

我需要DESC(MemorySize)命令的输出,如:

 我10 MB
Ť6 MB
第3 MB


解决方案

  $ OUT =阵列(); 的foreach($磁盘空间为$关键=> $值)//解析原数组
 {
     $行= array_values​​($值);
     $行= $行[0];
     preg_match('/ =(\\ d +)\\ ./',$行['数据'],$匹配); //获取数值
     // MB因为你并没有表现出什么别的可
     $出[] =阵列($键,$匹配[1] + 0'MB'); //转换$匹配[1]为整
 } uasort($出,函数($ A,$ B){//排序的第二列
    返回$一个[1]; $ B [1]? 1:-1;
 }); 的foreach($出来为$行)//输出排序的数据
    呼应加入('',$行)。 \\ n;

my code here: i try to use sort function but not success. this is my working array code :

MY code desplay output not sorted order:

<?php

 foreach($aCities as $key=>$val)
 {
     foreach($diskspace[$val] as $key=>$val2)
     {
         if(isset($val2['data']))
         {
             //'MBytes %.f'=155.000000%;
             preg_match("/'(.*?)ytes \%.f'=(.*?)\%;/",$val2['data'],$aVal);
             echo($val.' '.ceil($aVal[2]).' '.$aVal[1].'<br />');
         }
     }
 }
?>

Code OUTPUT:

S 3 MB
T 6 MB
I 10 MB

i need output in DESC(MemorySize) order like :

I 10 MB
T 6  MB
S 3  MB

解决方案

 $out = array();

 foreach($diskspace as $key => $value) // parse original array
 {
     $row = array_values($value);
     $row = $row[0];
     preg_match('/=(\d+)\./', $row['data'], $match); // get numerical value
     // MB as you did not show what else can be    
     $out[] = array($key, $match[1] + 0, 'MB'); // convert $match[1] to integer 
 }

 uasort($out, function ($a, $b) {  // sort by the second column
    return $a[1] < $b[1] ? 1 : -1; 
 });

 foreach($out as $row) // output sorted data
    echo join(' ', $row) . "\n";

这篇关于在倒序PHP数组排序出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆