Jump to content

Getting count of JSON output

hyperj123
Go to solution Solved by vorticalbox,

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. 

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 comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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 { signature } from "./signature";

signature.display({ cpu"Intel i9 9900k @ 3.56HZ",  gpu"ASUS GTX 1080",  ram"32GB CORSAIR DDR4", motherboard"ASUS Z390-E" });

 

Link to comment
Share on other sites

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 comment
Share on other sites

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

×