Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
hyperj123

Getting count of JSON output

Just get the length of 

cpanelresult.data

 

@NowThatsDamp no here is here for posting questioins. Stack is not the be all and end all of places to ask. 

Recommended Posts

Posted · Original PosterOP

I have a json output of:

{  
   "cpanelresult":{  
      "apiversion":2,
      "event":{  
         "result":1
      },
      "module":"MysqlFE",
      "func":"listdbs",
      "data":[  
         {  
            "size":"0",
            "db":"number1",
            "userlist":[  
               {  
                  "db":"number1",
                  "user":"user1"
               }
            ],
            "sizemeg":"0.00",
            "usercount":1
         },
         {  
            "usercount":1,
            "sizemeg":"0.00",
            "db":"number2",
            "userlist":[  
               {  
                  "user":"user2",
                  "db":"number2"
               }
            ],
            "size":"2720"
         },
         {  
            "size":"689144",
            "userlist":[  
               {  
                  "db":"number3",
                  "user":"user3"
               }
            ],
            "db":"number3",
            "sizemeg":"0.66",
            "usercount":1
         }
      ]
   }
}

How do I count using php to return a value of 3 so it counts each db?

Edit here is my current php code:
 

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_HEADER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
    curl_setopt($curl, CURLOPT_URL, $query2);
    $result2 = curl_exec($curl);
    curl_close($curl);
		$jsdb = json_decode($result, true);
		$dbdata = $jsdb['db'];
	echo $result2;
	echo count($dbdata);

 

Link to post
Share on other sites
Posted · Best Answer

Just get the length of 

cpanelresult.data

 

@NowThatsDamp no here is here for posting questioins. Stack is not the be all and end all of places to ask. 


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
<?php

$obj = '{
   "cpanelresult":{  
      "apiversion":2,
      "event":{  
         "result":1
      },
      "module":"MysqlFE",
      "func":"listdbs",
      "data":[  
         {  
            "size":"0",
            "db":"number1",
            "userlist":[  
               {  
                  "db":"number1",
                  "user":"user1"
               }
            ],
            "sizemeg":"0.00",
            "usercount":1
         },
         {  
            "usercount":1,
            "sizemeg":"0.00",
            "db":"number2",
            "userlist":[  
               {  
                  "user":"user2",
                  "db":"number2"
               }
            ],
            "size":"2720"
         },
         {  
            "size":"689144",
            "userlist":[  
               {  
                  "db":"number3",
                  "user":"user3"
               }
            ],
            "db":"number3",
            "sizemeg":"0.66",
            "usercount":1
         }
      ]
   }
}';


$json = json_decode($obj, true);
$count = count($json["cpanelresult"]["data"]);

echo "Count is: $count";

?>

Like @NowThatsDamp said, just get the count of cpanelresult.data and you will get the value you are looking for.


import * as signature from "signature";

signature.display({"CPU""Intel i9 9900k @ 3.56HZ",  "GPU"ASUS GTX 1080",  "RAM""16GB CORSAIR DDR4", "MOBO""ASUS Z390-E"});

 

Link to post
Share on other sites
5 hours ago, Apextier said:

<?php

$obj = '{
   "cpanelresult":{  
      "apiversion":2,
      "event":{  
         "result":1
      },
      "module":"MysqlFE",
      "func":"listdbs",
      "data":[  
         {  
            "size":"0",
            "db":"number1",
            "userlist":[  
               {  
                  "db":"number1",
                  "user":"user1"
               }
            ],
            "sizemeg":"0.00",
            "usercount":1
         },
         {  
            "usercount":1,
            "sizemeg":"0.00",
            "db":"number2",
            "userlist":[  
               {  
                  "user":"user2",
                  "db":"number2"
               }
            ],
            "size":"2720"
         },
         {  
            "size":"689144",
            "userlist":[  
               {  
                  "db":"number3",
                  "user":"user3"
               }
            ],
            "db":"number3",
            "sizemeg":"0.66",
            "usercount":1
         }
      ]
   }
}';


$json = json_decode($obj, true);
$count = count($json["cpanelresult"]["data"]);

echo "Count is: $count";

?>

Like @NowThatsDamp said, just get the count of cpanelresult.data and you will get the value you are looking for.

But I said that :(


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Buy VPN

×