zoukankan      html  css  js  c++  java
  • 修改数组的值和多维数组排序

    foreach($ret as $key => $value) {  //将数组元素中为空地补0,且添加sum元素的计算,注意修改数组的值,需要$key定位到是数组的哪一个地方需要修改,而不能简单地修改$value的值,因为$value只是一个临时的变量而已
        $sum = 0;
        if (isset($value['new_amount'])) {
            $sum += $value['new_amount']; 
        } else {
            //$value['new_amount'] = 0; 
            //$value['new_amount'] = 0;
            $ret[$key]['new_amount'] = 0;
            $ret[$key]['new_amount'] = 0;
        }
        if (isset($value['level_10_amount'])) {
            $sum += $value['level_10_amount'];
        } else {
            //$value['level_10_amount'] = 0;
            //$value['level_10_amount'] = 0;
            $ret[$key]['level_10_amount'] = 0;
            $ret[$key]['level_10_amount'] = 0;
        }
        if (isset($value['continus_login_amount'])) {
            $sum += $value['continus_login_amount'];
        } else {
            //$value['continus_login_amount'] = 0;
            //$value['continus_login_amount'] = 0;
            $ret[$key]['continus_login_amount'] = 0;
            $ret[$key]['continus_login_amount'] = 0;
        }
        if ($sum % 10 != 0) {
            $sum = $sum + (10 - $sum % 10);
        }
        //$value['sum'] = $sum;
        $ret[$key]['sum'] = $sum;
    }
    uasort($ret, 'my_sort'); //按元素sum进行排序
    function my_sort($a, $b) { //排序
        if ($a['sum'] == $b['sum']) return 0;
        return ($a['sum'] > $b['sum']) ? -1 : 1; //这里可以决定是倒序排,还是正序排
    }

     

    数组按照键id的值进行升序排序

    <?php 
    $array[] = array('id'=>1,'price'=>50);
    $array[] = array('id'=>2,'price'=>70);
    $array[] = array('id'=>3,'price'=>30);
    $array[] = array('id'=>4,'price'=>20);
    foreach ($array as $key=>$value){
      $id[$key] = $value['id'];
      $price[$key] = $value['price'];
    }
    array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array);
    ?>
  • 相关阅读:
    Eclipse Ctrl C卡的解决方法
    oracle函数 逗号分隔的字符串的每个字串两边加上单引号
    EXT自定义颜色表单域控件
    Ext JS开发经验累积
    mysql连接远程数据库
    Hibernate使用连接池
    oracle转mysql数据库 (hibernate+spring)
    解决html中img标签src中含中文出错的问题
    ckeditor自定义图片上传,结合EXT JS
    Web自动化框架搭建——前言
  • 原文地址:https://www.cnblogs.com/longzhongren/p/5067054.html
Copyright © 2011-2022 走看看