簡體   English   中英

php json多維數組輸出

[英]php json multidimentional array output

我正在嘗試創建 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);

但它只是循環它沒有顯示服務器一二和三之類的。

如何以上述給定的 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!");
        }
    }

作為初學者的我很難有這樣的想法,但我終於正確理解了。

如果將來有人需要幫助,請將其用作參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM