0

any ideas? in the second case $v is an array I could do is_array($v), but I'd have to repeat the if/else anyway

foreach(array('city', 'location') as $f) {
        $ors[$f] = array();

        if (!isset($_POST[$f])) continue;

        $v = $_POST[$f];
        if (isset($df[$f][$v])) {
                array_push($ors[$f], $df[$f][$v]);
        }
        else {
                security_error();
        }
}

foreach(array('age', 'sex') as $f) {
        $ors[$f] = array();

        if (!isset($_POST[$f])) continue;

        foreach($_POST[$f] as $v) {
                if (isset($df[$f][$v])) {
                        array_push($ors[$f], $df[$f][$v]);
                }
                else {
                        security_error();
                }
        }
}
1
  • I don't quite like how the code is set up. What does security_error() do? Also, what is $df? Commented Aug 4, 2011 at 4:23

2 Answers 2

2

Use ternary conditional operator for both loops:

$result = isset($df[$f][$v]) ? array_push($ors[$f], $df[$f][$v]) : security_error();

and/or for the function:

function pushIfSetOrSecurityError($source, $target, $key) {
    $result = isset($source[$key]) ? array_push($target, $source[$key]) : security_error();
}
Sign up to request clarification or add additional context in comments.

Comments

1
function pushIfSetOrSecurityError($source, $target, $key) {
        if (isset($source[$key]))
                array_push($target, $source[$key]);
        else
                security_error();
}

foreach(array('city', 'location') as $f) {
        $ors[$f] = array();
        if (isset($_POST[$f]))
                pushIfSetOrSecurityError($df[$f], $ors[$f], $_POST[$f]);
}

foreach(array('age', 'sex') as $f) {
        $ors[$f] = array();
        if (isset($_POST[$f]))
                foreach($_POST[$f] as $v)
                        pushIfSetOrSecurityError($df[$f], $ors[$f], $v);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.