Home  >  Q&A  >  body text

如何更优雅的写这段代码

<?php
$taskArray = array('1', '2', '3', '4', '5');
$deviceArray = array('Coolpad8705', 'HM2014011');
foreach($taskArray AS $t) {
    $task = $model->get($t);
    $a = false;
    foreach($deviceArray AS $device) {
        if ($model->is($device) == false) {
            $a = true;
            break;
        }
    }
    if ($a === false) continue;
}


高洛峰高洛峰2865 days ago702

reply all(5)I'll reply

  • 三叔

    三叔2016-11-16 13:21:46

    foreach($taskArray AS $t) {
        $task = $model->get($t);
        // $a = false;
        // foreach($deviceArray AS $device) {
        //     if ($model->is($device) == false) {
        //         $a = true;
        //         break;
        //     }
        // }
        // if ($a === false) continue;
        if ( ! in_array($model->device(), $deviceArray))
            // do something
    }


    reply
    0
  • 欧阳克

    欧阳克2016-11-16 13:21:30

    get(5);


    reply
    0
  • 三叔

    三叔2016-11-16 13:21:17

    $model->is 是什么作用?

    reply
    0
  • 欧阳克

    欧阳克2016-11-16 13:21:07

    get($t);//What's this ?
        $a = false;
        foreach($deviceArray AS $device) {
            if ($model->is($device) == false) { //false == $model->is($device)
                $a = true;
                continue; //break
            }
        }
        if ($a === false) continue; //delete this
    }


    reply
    0
  • 三叔

    三叔2016-11-16 13:20:52

    两句continue;是没用的。
    请区分continue;和break;的效果。
    不写这两句没有任何影响,换成break;可以减少循环次数。

    优雅什么的……
    简洁、有效、有注释就好。
    IDE会自动处理好格式的。


    reply
    0
  • Cancelreply