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 回答