隐藏foreach循环中的数组项

时间:2018-03-14 11:12:40

标签: php

我有一个从我服务器上的文件中提取的项目列表。

我隐藏了某些文件名,正如您在我的代码中看到的那样,但变量我正在使用" $ filelist"只隐藏数组中的最后一个值而不是所有值。

如下图所示,A15和A17应隐藏,但只有A17。

如何使用$ filelist隐藏其中的所有值,而不是仅隐藏最后一个?

 public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions oAuthOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20),
            Provider = new ApplicationOAuthProvider()
        };
        app.UseOAuthAuthorizationServer(oAuthOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
        {
            Provider = new OAuthBearerAuthenticationProvider()
        });

        HttpConfiguration config = new HttpConfiguration();
        //config.Filters.Add(new );
        //config.MapHttpAttributeRoutes();
        // There can be multiple exception loggers. (By default, no exception loggers are registered.)
        //config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
        WebApiConfig.Register(config);
        //enable cors origin requests
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config);            
    }
}


 public static class WebApiConfig
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="config"></param>
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Filters.Add(new HostAuthenticationAttribute("bearer")); //added this
        config.Filters.Add(new AuthorizeAttribute());
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }
        );

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    }

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var form = await context.Request.ReadFormAsync();
        if (myvalidationexpression)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Role, "AuthorizedUser"));
            context.Validated(identity);
        }
        else
        {
            context.SetError("invalid_grant", "Provided username and password is incorrect");
        }
    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

将要隐藏的文件名添加到$filelist数组,然后使用$file检查in_array是否在该数组中

$filelist = [];
while ($row = mysql_fetch_assoc($result)) {
    $filelist[] = $file = $row["file"];
    echo "<input type=\"checkbox\" value=\"$file\" name=\"files[]\" checked=\"checked\"/>&nbsp;&nbsp;$file<br />";
}
$filelist[] = ".";
$filelist[] = "..";
$filelist[] = "index.php";
$filelist[] = "Site Induction Sheet.docx";
$filelist[] = "Method Statement Complete.docx";
$filelist[] = "Construction Phase Plan Complete.docx";
$filelist[] = "Method Statement.docx";
$filelist[] = "Construction Phase Plan.docx";
echo "<br>";
$dirname = "/var/www/vhosts/hub.gkrmaintenance.co.uk/public_html/forms/templates";
$forms = scandir($dirname);
sort($forms);
foreach ($forms as $file) {
    if (!in_array($file, $filelist)) {
        echo "<input type=\"checkbox\" value=\"$file\" name=\"files[]\" />&nbsp;&nbsp;$file<br />";
    }
}

答案 1 :(得分:1)

您的$filelist不是列表,它只是列表中的最后一项。您可以使用其他答案中看到的in_array()方式,或! isset()方式:

$file_seen = array();

while ($row = mysql_fetch_assoc($result)) {
     $file = $row["file"];
     echo "<input type=\"checkbox\" value=\"$file\" name=\"files[]\" checked=\"checked\"/>&nbsp;&nbsp;$file<br />";
     $file_seen[ $file ] = true;
}

…

foreach ($forms as $file) {
  if( … and
    ( ! isset( $file_seen[ $file ])
  ){
       echo "<input type=\"checkbox\" value=\"$file\" name=\"files[]\" />&nbsp;&nbsp;$file<br />";
   }
}