0

对于“提交”输入项:

name="remove['.$username.']['.$event_id.']" 

if (isset($_POST[remove])) {
    foreach ($_POST[remove] as $username => $event_obj) {
        foreach ($event_obj as $event_id => $obj) {
            delete .... WHERE event_id='$event_id' AND username='$username'");
        }
    }
}

是否有更短的方法来获取在具有一个项目的数组中使用的关联键?

4

1 回答 1

0

用于一次array_keys()获取所有event_id值。这也只需要对每个用户名进行一次查询。

if( isset( $_POST['remove'])) {
    foreach( $_POST['remove'] as $username => $event_obj) {
        $event_ids = array_map( 'intval', array_keys( $event_obj));
        $sql = 'DELETE FROM mytable 
                    WHERE event_id IN ( ' . implode( ' OR ', $event_ids) . ' ) 
                        AND username = "' . $username  . '"'; // Sanitize $username
    }
}
于 2013-02-12T02:21:12.123 回答