dtukyb8095 2016-04-29 02:29
浏览 48
已采纳

在PHP中添加字符串不起作用

I'm making a bot that returns Mojang's server status. Here's the code.

$scan = json_decode(file_get_contents("https://status.mojang.com/check"), true);

$errors = array();

if ($scan[0]["minecraft.net"] !== "green") {
    array_push($errors, "Minecraft is down: ");
}

if ($scan[1]["session.minecraft.net"] !== "green" || $scan[6]["sessionserver.minecraft.net"] !== "green") {
    array_push($errors, "All server sessions are down. ");
}

if ($scan[2]["account.minecraft.net"] !== "green") {
    array_push($errors, "Account services are down. ");
}

if ($scan[3]["auth.minecraft.net"] !== "green" || $scan[5]["authserver.minecraft.net"] !== "green") {
    array_push($errors, "Authentication servers are down. ");
}

if ($scan[4]["skins.minecraft.net"] !== "green") {
    array_push($errors, "Skin servers are down. ");
}

$message = "Sometimes it's Mojang... ";

if (sizeof($errors) !== 0) {
    foreach ($errors as $i) {
        $message .= $errors[$i];
    }
} else {
    $message .= "but today, it seems like all of Mojang's servers are working fine!";
}

echo $message;

Whenever I execute this in my terminal, it outputs "Sometimes it's Mojang... ", and that's it. There's nothing else. Either way, if the if is true or false, it will add something to the string, so it should be printing more than that, but it isn't. Any help would be appreciated, and sorry if this is an easy fix.

Thank you!

  • 写回答

2条回答 默认 最新

  • doubo6658 2016-04-29 02:37
    关注

    Two ways

    You are passing the value as keys then the value is not found

    use key instead of val

     foreach ($errors as $i => $val) {
        $message .= $errors[$i];
     }
    

    Or use direct value

     foreach ($errors as $i) {
        $message .= $i;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致