3

私はこれがArray好きです:

array(10) { [0]=> object(stdClass)#3 (4) { ["name"]=> string(2) "OA" ["datetime"]=> string(10) "1990-05-10" ["amount"]=> string(2) "50" ["comment"]=> string(9) "Something" } [1]=> object(stdClass)#4 (4) { ["name"]=> string(1) "O" ["datetime"]=> string(10) "1992-10-01" ["amount"]=> string(2) "20" ["comment"]=> string(5) "Other" } ...

そして、マークされた配列で情報を検索しphpます。多くのフィルター(その配列で定義されてHTMLいるすべてのパラメーター)を含むフォームがあります。これが私のphpスクリプトです:

if (isset($_POST['action'])){ // if form is submitted
    $name   = $_POST['name'];
    $amount = $_POST['amount'];
    // like I've mentioned I've more filters but to simplify example lets have two
    if ($name!="" && $amount!=""){ // search by both param
        foreach($arr as $obj){ // $arr is the marked array from above
            if ( (strpos(strtolower($obj->name),strtolower($name))!==false) && ($amount >= $obj->amount) ){
                $new[] = (object)array(
                    "name" => $obj->name,
                    "datetime" => $obj->datetime,
                    "amount" => $obj->amount,
                    "comment" => $obj->comment
                );
            }
        }
        print_r($new);
    } elseif ($name=="" && $amount!=""){ // only by amount
        // same foreach loop goes here with difference that I'm only searching by amount
    } elseif ($name!="" && $amount==""){ // only by name
        // same foreach loop goes here with difference that I'm only searching by name
    }
    // etc ...
}

すべてが正常に機能していますが、非常に多くのifステートメントを短縮して目的を達成するための別の簡単な方法に興味があります。アドバイスをありがとう ...

4

1 に答える 1

3

確かに短くは見えませんが、それは他のforeachループを省略したためです。これは、重複コードがないようにするための最初のリファクタリングです。

        $arr = arrayThing();
        $new = array();
        if (isset($_POST['action']))
        { // if form is submitted
            $name   = isset($_POST['name']) ? $_POST['name'] : '';
            $amount = isset($_POST['amount']) ? $_POST['amount'] : '';
            if ($name != '' && $amount != '')
            {
                $searchBy = 'both';
            }
            elseif ($name == '' && $amount != '')
            {
                $searchBy = 'amount';
            }
            else
            {
                $searchBy = 'name';
            }

            foreach ($arr as $obj)
            {
                $valid = false;
                switch ($searchBy)
                {
                    case 'both':
                        if ((strpos(strtolower($obj->name), strtolower($name)) !== false) && ($amount >= $obj->amount))
                        {
                            $valid = true;
                        }
                        break;
                    case 'amount':
                        if ($amount >= $obj->amount)
                        {
                            $valid = true;
                        }
                        break;
                    case 'name':
                        if (strpos(strtolower($obj->name), strtolower($name)) !== false)
                        {
                            $valid = true;
                        }
                        break;
                    default:
                        break;
                }

                if ($valid)
                {
                    $new[] = (object) array(
                                "name"     => $obj->name,
                                "datetime" => $obj->datetime,
                                "amount"   => $obj->amount,
                                "comment"  => $obj->comment,
                    );
                }
            }
            print_r($new);
        }
于 2013-03-11T19:46:13.520 に答える