doumen5491 2014-09-24 20:26
浏览 27
已采纳

与Yii框架一起使用

To be honest i have two problems here, one im new to Yii framework and the usort functions i have found on the net do not make sense to me. I havent found one that explains in laymans terms what is going on.

In short i have an object array something like this:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [name] => Mary Jane
        [count] => 420
    )

[1] => stdClass Object
    (
        [id] => 4
        [name] => Johnny
        [count] => 234
    )

[2] => stdClass Object
    (
        [id] => 3
        [name] => Kathy
        [count] => 4354
    )

.... I want to sort the object by id The problem is the data is sorted by date it was last updated rather than ID and i need to know the last ID because what i do is pull the data from the API into my database so im essentially checking to see the last API id is 1100 but my database last id is 1050 so i know im approx 50 records behind.

So i have created a public static function in the activity Model

public static function sort_api_data_by_id($a, $b)
    {
        return strcmp($a->id, $b->id);
    }

And in one of my pages i am running the usort functions

usort($array, Activity::model()->sort_api_data_by_id());

If it makes a difference, the file is a view under activity so its accessing the function in the activity model so its not a different model/view relationship or anything.

I got that function from another page but what i don't understand is what i need to pass to the function for parameters $a and $b, in none of the examples does it seem to pass anything. sort_api_data_by_id is the function in the activity model.

Now may while it might be more efficient to just find the highest id rather than sort the whole object i still need to process the data later and enter it into the database and thats going to be easier if its in a logical order.

  • 写回答

1条回答 默认 最新

  • dongzi0850 2014-09-25 01:38
    关注

    I eventually found the solution hidden within google.

    I put this static function in the Activity Model:

    public static function sort_api_data_by_id($a, $b){
        if     ($a->id == $b->id) return 0;
        return ($a->id <  $b->id) ? -1 : 1;
    }
    

    And then i call it with ClassName::function_name but you drop the ($a,$b). Thats what initially threw me was it didnt make sense there were parameters in the function but we werent passing any but i believe we are passing them in a roundabout way through usort and $a is the array and $b is the result of the function i.e is the id higher or lower although i could be wrong as its a bit difficult to get your head around so please take this as my understanding and not fact. I have not really seen it explained full anywhere else.

    In any case this usort function then works by passing it the array and the static function

    usort($array, "Activity::sort_api_data_by_id"); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题