php json多维数组输出

php json多维数组输出,php,json,phpmyadmin,Php,Json,Phpmyadmin,我正在尝试创建api来显示我的服务器状态 最近,我创建了一个非常粗糙的api,你可能会笑得太多,这里是我所做的代码 $Status2 = "Failed"; $Status3 = "Failed"; $Status4 = "Failed"; $info1 = "Opps! looks like our Se

我正在尝试创建api来显示我的服务器状态

最近,我创建了一个非常粗糙的api,你可能会笑得太多,这里是我所做的代码

            $Status2 = "Failed";
            $Status3 = "Failed";
            $Status4 = "Failed";
            
            $info1 = "Opps! looks like our Server One is Down. Please contact our support team for more information";
            $info2 = "Opps! looks like our Server Two is Down. Please contact our support team for more information";
            $info3 = "Opps! looks like our Server Three is Down. Please contact our support team for more information";
            $info4 = "Opps! looks like our Server Four is Down. Please contact our support team for more information";
            
            $serverOne = "http://firstserver.com";
            $serverTwo = "http://2ndserver.com";
            $serverThree = "http://3rdserver.com";
            $serverFour = "http://4thserver.com";
            
            // Takes raw data from the request
            $jsonOne = file_get_contents($serverOne);
            $jsonTwo = file_get_contents($serverTwo);
            $jsonThree = file_get_contents($serverThree);
            $jsonFour = file_get_contents($serverFour);

            // Converts it into a PHP object            
            $serverOneBody = @json_decode($jsonOne, true);
            $serverTwoBody = @json_decode($jsonTwo, true);
            $serverThreeBody = @json_decode($jsonThree, true);
            $serverFourBody = @json_decode($jsonFour, true);
            
            $statusOne = $serverOneBody["status"];
            $statusTwo = $serverTwoBody["status"];
            $statusThree = $serverThreeBody["status"];
            $statusFour = $serverFourBody["status"];
            
            if($statusOne == 1){$Status1 = "Success"; $info1 = "Success! Server One is live and working!";}
            if($statusTwo == 1){$Status2 = "Success"; $info2 = "Success! Server Two is live and working!";}
            if($statusThree == 1){$Status3 = "Success"; $info3 = "Success! Server Three is live and working!";}
            if($statusFour == 1){$Status4 = "Success"; $info4 = "Success! Server Three is live and working!";}
            
            $respon = array(    "serverOne" => array ('response' => 200,'status' => $Status1,'info' => $info1),
                                "serverTwo" => array ('response' => 200,'status' => $Status2,'info' => $info2),
                                "serverThree" => array ('response' => 200,'status' => $Status3,'info' => $info3),
                                "serverFour" => array ('response' => 200,'status' => $Status4,'info' => $info4)
                            );
                                
$this->response($this->json($respon), 200);
我知道它很生

我希望在中接收json

{
        "serverOne": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server One is live and working!"
        },
        "serverTwo": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Two is live and working!"
        },
        "serverThree": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Three is live and working!"
        },
        "serverFour": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Four is live and working!"
        }
    }     
现在我正在努力

$items = array();
            
            $servers = array(
            "one" => "sv1.com", 
            "two" => "sv2.com",
            "three" => "sv3.com"
            ); 

            foreach ($servers as $server) {
                $json = file_get_contents($server);
                // Converts it into a PHP object            
                $jsonBody = @json_decode($json, true);
                $status = $jsonBody["status"];
                
                if($status == 1){
                    array_push($items,"Success");
                    array_push($items,"Success! Server number is live and working!");
                    }else{                  
                    array_push($items,"Error");
                    array_push($items,"Error! Server number is Not working properly!");                 
                }               

                
                
            }
            $this->response($this->json($items), 200);
但它只是循环,没有显示服务器1、2和3

如何获得上述给定json格式的可能输出

请帮帮我,我在网上到处搜索,但没有找到答案

我现在收到的代码是

[
    "Success",
    "Success! Server number is live and working!",
    "Success",
    "Success! Server number is live and working!",
    "Error",
    "Error! Server number is Not working properly!",
    "Success",
    "Success! Server number is live and working!"
]


过了很长时间,我终于明白了

$respon = array();

foreach (array_values($servers) as $id => $url) {
        $json = file_get_contents($url);
        // Converts it into a PHP object            
        $jsonBody = @json_decode($json, true);
        $status = $jsonBody["status"];
                        
    if($status=='1'){
       $respon[$id]= array("response"=>"$headers[0]","status"=>"Success","info"=>"Server $id is Live and Working Fine!");
        }else{
        $respon[$id]= array("response"=>"$headers[0]","status"=>"Failed","info"=>"Server $id is not Working!");
        }
    }
作为一个初学者,这样思考对我来说并不容易,但我最终正确地理解了它


如果将来有人需要帮助,请将其作为参考。

仍然没有人帮助吗?