idssphp在foreach中复制数组

时间:2015-05-13 15:54:42

标签: php arrays foreach

行...

你的答案很好@Random,但是它仍然在重复ID,所以我将你的代码集成到我的开发代码中...所以这是我的最终代码....

$gr = mysql_query("select id from permission_sets");
$permission_arr = array(
'start_perms'  => isset($_POST['START_'.$id]),
'reply_perms'  => isset($_POST['REPLY_'.$id]),
'read_perms'   => isset($_POST['READ_'.$id]),
'upload_perms' => isset($_POST['UPLOAD_'.$id]),
'show_perms'   => isset($_POST['SHOW_'.$id]),
'download_perms' => isset($_POST['DOWNLOAD_'.$id]));

// saving the beginning of the line : "group: 1,2,3"
$header = 'PID: ';
$read   = "";
$reply  = "";
$start  = "";
$upload = "";
$download = "";
$show   = "";
$subPIDs = "";
while($row = mysql_fetch_array($gr)){
foreach($row as $pid){
//-----------------------------
// Get perm id and set it..
//-----------------------------
$pid = $pid.",";


if($permission_arr['start_perms'] == 1){
$start .= $pid;
$subPIDs .= $start;
}
if($permission_arr['read_perms'] == 1){
$read .= $pid;
$subPIDs .= $read;
}
if($permission_arr['reply_perms'] == 1){
$reply .= $pid;
$subPIDs .= $reply;
}
if($permission_arr['upload_perms'] == 1){
$upload .= $pid;
$subPIDs .= $upload;
}
if($permission_arr['download_perms'] == 1){
$download .= $pid;
$subPIDs .= $download;
}
if($permission_arr['show_perms'] == 1){
$show .= $pid;
$subPIDs .= $show;
}
$header .= $subPIDs;
}

} // End while

//------------------------------------
// Store the data in a new array..
//------------------------------------
$perm_arr = array(
'start_perms'  => $start,
'reply_perms'  => $reply,
'read_perms'   => $read,
'upload_perms' => $upload,
'show_perms'   => $show,
'download_perms' => $download);
$head = $header.' = ';
foreach($perm_arr as $key => $val){
if($val){
$head .= $key.'<br>'.$head;
}
}

print $head;
echo '<pre>';
print_r($perm_arr);

这就是你所做的一切...我们只需要在输出中停止欺骗,在阵列中没有欺骗,只在输出中...当我只标记1个盒子时,阵列得到全部或没有ids ..

1 个答案:

答案 0 :(得分:0)

试试这个:

// list of permissions with boolean
$permission_arr = array(
    'start_perms'  => isset($_POST['START_'.$id]),
    'reply_perms'  => isset($_POST['REPLY_'.$id]),
    'read_perms'   => isset($_POST['READ_'.$id]),
    'upload_perms' => isset($_POST['UPLOAD_'.$id]),
    'show_perms'   => isset($_POST['SHOW_'.$id]),
    'download_perms' => isset($_POST['DOWNLOAD_'.$id]));

echo 'sql';
$gr = mysql_query("select id from permission_sets");
// saving the beginning of the line : "group: 1,2,3"
$header = 'group: ';
$separator = '';
while($row = mysql_fetch_array($gr)) {
    foreach($row as $g){
        $header .= $separator.$g;
        $separator = ',';
    }
}
$header = $header.' = ';
foreach($permission_arr as $k => $p){
    if($p) {
        // the permission is OK, we print this permission
        // output like "group: 1,2,3 = start_perms, <br>"
        print $header.$k.", <br>";
    }
}